The environment

Java1.8, idea2018, springboot

To help people

Java software engineer, Java backend development, Java software development ready for interview, new features in jdk1.8

Learning goals

Master the most common usage of Java Lambda function programming filter, map, etc.

You can also write your own Lambda function code by referring to the sample code;

I’ve written Lambda functional programming before, and I’m adding to that.

The code for

The most common examples of JDK1.8 Lambda functional programming in application development are as follows

Preparing Test data

conversion

map

Transform plus filter 1

map+filter

Conversion plus filtering 2

map+object

Conversion plus filtering 3

map+entryset

Matches a specified entry

 

Prevent NPE1

Option.ofnullable (collection object).orelse (collection object)

 

Note the default null values. The following two are recommended: an empty set and a null character.

The new collection consumes resources.

 

Prevent NPE2

In addition to the above anti-collection object null, there is also anti-collection element null

Using the filter (Objects: : nonNull)

 

The map traverse

It’s a little bit simpler

 

The list to heavy

Stream ().distinct()

 

Method references

Above, filter(Objects::nonNull) is a method reference, and the code is at its simplest

 

Nested loop flat words

I don’t use much of it

 

 

 

father.stream().map(o->o.getChildAs()).flatMap(List::stream)
 .map(gg->gg.getName()).forEach(
 System.out::println);
Copy the code

Pay attention to

Both filter and map can be used consecutively together.

Such as the filter (XXX). The filter (XXXX)

map(XXX),map(XXXX)

Streaming syntax, programmers learn it