I've reviewed Java's lambda expression, stream API, six months after I started Java.

What is a lambda expression after all?

** Technology introduced from Java SE 8 **

Lambda expressions allow you to write multithreaded programs with multiple processor cores in today's computers with significantly simpler code than ever before.

Quote Java programming is so simple with lambda expressions and stream APIs! --Why you should move to Java SE 8 now https://builder.japan.zdnet.com/sp_oracle/35065645/

Using a lambda expression seems to make coding simple.

For the time being, I feel like I can definitely use it when I can use a lambda expression.

Suppose the list contains the following data:

From this data, people aged 40 and over are extracted and output in ascending order of age. Suppose you write a program called.

List<Musician> users = Arrays.asList(
new Musician(1, "Oyamada", "Sohei", 34, "Fukuoka Prefecture"),
new Musician(2, "Kishida", "Traditional", 42, "Kyoto"),
new Musician(3, "Mukai", "Hidenori", 45, "Fukuoka Prefecture"),
new Musician(4, "Fujiwara", "Motoo", 39, "Chiba"),)

This is also a quote, but up to Java SE 7

Below for data extraction over 40 years old

Set<Musician> musician = new HashSet<>();
for (Musician user : users) {
    if (user.getAge() > 40 ) {
        musisian.add(user);
    }
}

Below to sort the extracted data

List<Musician> sorted = new ArrayList<>(musician);
Collections.sort(sorted, new Comparator<Musician>() {
    public int compare(Musician m1, Musician m2) {
        return Integer.compare(m1.getAge(), m2.getAge());
    }
})

Below in the output of the sorted data

for (Musician user : sorted) {
    System.out.println(user.getFirstName());
}

I will code it like this.

For the processing that you want to realize, you are declaring a new variable that holds the sort result in the intermediate processing.

If you try to parallelize this process, if you declare a variable in the intermediate process, synchronous processing will be required, performance will decrease, and parallelization will become more difficult to achieve.

The maintainability of the code is also not very good.

While the essential processing is extraction, sorting, and result output, there are many intermediate processing.

** So Java SE 8 **

users.Stream()
.filter(u -> u.getAge() > 40)
.sorted((m1, m2) -> Integer.compare(m1.getAge(), m2.getAge()))
.forEach(user -> System.out.println(user.getFirstName()));

You can code simply and overwhelmingly sensuously.

What is the Stream API

You can easily realize parallel processing! That guy

users.parallelStream()
.filter(u -> u.getAge() > 40)
.sorted((m1, m2) -> Integer.compare(m1.getAge(), m2.getAge()))
.sequential()
.forEach(user -> System.out.println(user.getFirstName()));

users.parallelStream() You can easily implement parallel processing just by adding.

By the way, the following .sequential()

Means that output processing is not processed in parallel.

The above is a summary that also serves as a reminder.

Recommended Posts

I've reviewed Java's lambda expression, stream API, six months after I started Java.
Getting Started with Legacy Java Engineers (Stream + Lambda Expression)
Java8 stream, lambda expression summary
I tried using Java8 Stream API
Nowadays Java lambda expressions and Stream API
Java8 Lambda expression & Stream design pattern reconsideration --Command pattern -
[Java] Lambda expression
Java lambda expression
[In-house study session] Java basics-Lambda expression and Stream API- (2017/07/13)
[For beginners] How to operate Stream API after Java 8
Use Java lambda expressions outside of the Stream API
I tried Java Lambda input / output type ~ Stream version ~
Java8 Lambda Expression & Stream Design Pattern Rethinking --Null Object Pattern -
Java8 Lambda expression & Stream design pattern reconsideration --Template Method pattern -
java neutral lambda expression 1
[Java] Stream API / map
Java8 Stream API practice
Java 8 lambda expression Feature
java lambda expression memo
Java lambda expression [memo]
Studying Java 8 (lambda expression)
Review java8 ~ Lambda expression ~
Java lambda expression again
[Java improvement case] I started programming after I was 30 years old.
Handle exceptions coolly with Java 8 lambda expressions and Stream API
Java8 Lambda Expression & Stream Design Pattern Rethinking --Chain of Responsibility Pattern -
I was addicted to using Java's Stream API in Scala