JAVA8 learning notes - Common built-in interfaces and Stream intermediate operations

Keywords: Java Lambda

I'm ashamed that JAVA 8 has been released for 7 years, and I have been exposed to JAVA for more than 2 years. JDK14 has been released, and I'm still learning new features of JAVA 8, facing the wall for 3 minutes.

In the afternoon, I took the time to learn some common built-in interfaces and Stream intermediate operations of Java 8, and took notes of my study.

Supplier interface:
Generally speaking, it is a provider with only one get() method to output the execution result of the method body.

1 @FunctionalInterface //Express functional interface check,If not,Report wrong    
2 public interface Supplier<T> {
3     T get();
4 }

Because it is a functional interface, you can use lambda expressions:

1 str1.orElseGet(new Supplier<String>() {
2     @Override
3     public String get() {
4         return "123";
5     }
6 });
7     

It can be abbreviated as:

1 str1.orElseGet(() -> "123");

Examples:

Use in combination with Optional object: Optional is the object used by java8 to eliminate null pointer and wrap parameters

1 //Create a Optional object,Generics determine the data type of the wrapper
2 //ofNullable The parameter can be null
3 //Same,Optional.of(null)Parameter is not allowed null,Otherwise, a null pointer exception will be thrown
4 Optional<String> str1 = Optional.ofNullable(null);    
5 //orElseGet(Supplier Interface<? extends Definition Optional Generics of time)
6 //Indicate if str1 by null,Execute supplier Interface get Method
7 //Return type must be defined Optional Type or subclass of
8 String newStr = str1.orElseGet(() -> "123");

Consumer interface:

Consumer, there is only one abstract method of accept, passing in a value of generic type, operating on the value, not returning

1 @FunctionalInterface    
2 public interface Consumer<T> {
3     void accept(T t);
4 }

Examples:

Cooperate with java8's Stream and forEach to traverse data and operate. There will be a comprehensive use case behind

Function interface:

The combination of provider and consumer, with reference to return, generic < T, R > refers to the incoming T type, return R type

1 @FunctionalInterface
2 public interface Function<T, R> {
3     R apply(T t);
4 }

Examples:

Change the String type of array element to Integer type, add 10, and traverse (the Consumer interface is used for traversal)

Here, we use < R > stream < R > map (Function <? Super T,? Extends r > mapper). We can see that its parameter is the Function interface

The function of map is to put an array or collection into a stream, convert the type of elements, and return a stream

1 Stream<String> stream2 = Arrays.stream(new String[]{"1","2","3","4"});
2 stream2.map(x->Integer.valueOf(x)+10).forEach(System.out::println);

Predict interface:

Pass in an object and return a boolean value, which is generally used for judgment logic

1 @FunctionalInterface
2 public interface Predicate<T> {
3     boolean test(T t);
4 }

Examples:

Filter elements larger than 1, and then traverse the elements

Stream < T > filter (Predicate <? Super T > Predicate) is used here. Its parameter is the Predicate type

The function of filter is to make logical judgment on elements and return the result flow with boolean being true

Stream<String> stream1 = Arrays.stream(new String[]{"1","2","3","4"});
stream1.filter(x->Integer.valueOf(x) > 1).forEach(System.out::println);

In the above example, we have introduced the filter and map methods of Stream stream. Other intermediate operations are almost the same. Here we will introduce a special flatMap

filtMap:

  <R> Stream<R> flatMap(Function<? super T, ? extends Stream<? extends R>> mapper)

Pass in a Function function with two parameters. The second parameter is the Stream stream object

Combine Function function Function: convert parameter type, which is roughly: execute Function function Function, merge result flow into current flow, which is a Function of merging flow

Examples:

Cuts all array elements and traverses the output

 1 Stream<String> stream3 = Arrays.stream(new String[]{"Prosperous and strong","democratic","civilization","Harmonious"});
 2 //To string array elements String Separate cutting,What we got was String[],accord with map Rules for transition types
 3 //This time forEach Printing,Printed array
 4 stream3.map(x->x.split("")).forEach(System.out::println);//[Ljava.lang.String;@7085bdee
 5 
 6 //Correct usage
 7 //Use flatMap Implement merge flow
 8 Stream<String> stream4 = Arrays.stream(new String[]{"Prosperous and strong","democratic","civilization","Harmonious"});
 9 //Here,Equivalent to cutting four array elements separately,Form four arrays,Stream merge again,Put four arrays into the stream,Traversal
10 stream4.map(x->x.split("")).flatMap(x->Arrays.stream(x)).forEach(System.out::println);

Can understand the above examples, basically a lot of Java 8 code can understand, there are some Stream terminal operations, take time to learn

People who just watch and don't do will never learn!!!

For more information: https://blog.csdn.net/qq_/article/details/80962325

Posted by Buyocat on Sun, 05 Apr 2020 12:10:31 -0700