What's new in Java 8

Who?

@carimatics --Web application engineer ――I'm being played with Perl in business --Emacs Lisp I'm getting tired of Gonyo and crying I'm using Vim --Uncle Garupan --Now I'm living just to see the movie version released in December


Well, everyone


People using Java 8 or later: raised_hand:


People using new Java 8 features: raised_hand:


New features since Java 8

Lambda expression

--Syntax for writing anonymous functions easily --Improved readability --Internally a functional interface --The only interface that should be implemented

Stream API --Applying lambda expressions to collections - filter - map - reduce

etc...


I will write


Lambda expression


Returns the length of the string

Anonymous class version

Implementation

Function<String, Integer> countLen = new Function<>() {
    @Override
    public Integer apply(String s) {
        return s.length();
    }
};
int len = countLen.apply("Function class sample!!!");
System.out.println(len); // => 24

problem

――It's just long


Shave redundant things


Function<String, Integer> countLen = new Function<>() {
    @Override
    public Integer apply(String s) {
        return s.length();
    }
};

Function<String, Integer> countLen =
    @Override
    public Integer apply(String s) {
        return s.length();
    }
 ;

--The interface name to be implemented is self-explanatory --Funtion is written on the left side


Function<String, Integer> countLen =

                        (String s) {
        return s.length();
    }
 ;

--The return value is self-explanatory --Integer is written on the left side --Method name is self-explanatory --Since there is only one method to implement


Function<String, Integer> countLen = (String s)    { return s.length(); };

Organize


Function<String, Integer> countLen = (String s) -> { return s.length(); };

--Lambda expressions require a -> (arrow token) between the argument and the body


Function<String, Integer> countLen = (String s) ->          s.length()   ;

--If the body is a single sentence, you can omit return and curly braces.


Function<String, Integer> countLen =         s  ->          s.length()   ;

--Can be omitted if the argument type is known --String is written on the left side


Function<String, Integer> countLen = s -> s.length();

Organize (Although it can be changed to the method reference format, it is omitted due to time constraints)


Returns the length of the string

Anonymous class version

Function<String, Integer> countLen = new Function<>() {
    @Override
    public Integer apply(String s) {
        return s.length();
    }
};

Lambda version

Function<String, Integer> countLen = s -> s.length();

Stream API


The average value of the numbers in [1..100]

for statement

double sum = 0.0;
for (int i = 1; i <= 100; i++) {
    sum += i;
}
double ave = sum / 100;

Stream API

double ave = IntStream.rangeClosed(1, 100)
        .average()
        .orElse(0.0);

Average salary of men in their 30s among employees

for statement

double sumSalary = 0.0;
for (Employee employee : employees) {
    if (30 <= employee.getAge() && employee.getAge() < 40
            && employee.getGender() == Gender.MALE) {
        sumSalary += employee.getSalary();
    }
}
double a = employees.isEmpty() ? 0.0 : sumSalary / employees.size();

Stream API

double averageSalary =
        employees.stream()
                .filter(e -> 30 <= e.getAge() && e.getAge() < 40) //30s
                .filter(e -> e.getGender() == Gender.MALE)        //male
                .mapToInt(e -> e.getSalary())                     //Salary
                .average()                                        //average
                .orElse(0.0);                                     //0 if there is no applicable person.0

What's good with lambda expressions and the Stream API

--Can be written concisely --Improved readability --Easy to maintain

The Stream API function has been added to Java9 as well, which is very convenient!


Let's all write Kotlin! !!


Kotlin is the best! !!

Recommended Posts

What's new in Java 8
What's new in Java 9,10,11
What's new in Apache Camel 2.19.0
Partization in Java
Changes in Java 11
java1.8 new features
Pi in Java
FizzBuzz in Java
I tried the new era in Java
[java] sort in list
Read JSON in Java
Interpreter implementation in Java
Make Blackjack in Java
Java 12 new feature summary
Rock-paper-scissors app in Java
Constraint programming in Java
Put java8 in centos7
NVL-ish guy in Java
Combine arrays in Java
Callable Interface in Java
Java 13 new feature summary
Comments in Java source
Azure functions in java
Format XML in Java
Simple htmlspecialchars in Java
Boyer-Moore implementation in Java
Hello World in Java
Use OpenCV in Java
webApi memorandum in java
Ping commands in Java
Various threads in java
Heapsort implementation (in java)
Zabbix API in Java
ASCII art in Java
Compare Lists in Java
POST JSON in Java
Express failure in Java
Create JSON in Java
Date manipulation in Java 8
Java 10 new feature summary
Use PreparedStatement in Java
Java 14 new feature summary
Parallel execution in Java
Initializing HashMap in Java
Try using RocksDB in Java
[Java] New Thread generation method (2)
Read binary files in Java 1
Avoid Yubaba's error in Java
Get EXIF information in Java
Save Java PDF in Excel
[Neta] Sleep Sort in Java
Edit ini in Java: ini4j
Java history in this world
Let Java segfault in 6 lines
Try calling JavaScript in Java
Try developing Spresense in Java (1)
Try functional type in Java! ①
I made roulette in Java.
New features from Java7 to Java8
Implement two-step verification in Java
Refactoring: Make Blackjack in Java