Advanced Java: new features of JDK8: Stream, method reference

catalogue

Stream stream

Flow thinking:

Termination method

Delay method

Static method

Method of getting stream

Method reference

Specific use method

Stream stream

In the past, when operating on a collection, it was always loop, loop and recycle. Circulation is only a way, not an end. A disadvantage of linear loop is that it can only be traversed once. If there are other operations, you need to cycle again

Lambda's derivative Stream (only consider the purpose, not the implementation method)

Flow thinking:


Similar to the pipeline, the delay method in the middle will return a flow object (the flow object itself), and finally there will be a termination method

Stream stream objects can only be used once (each model (i.e. stream object) will be automatically destroyed after use)

characteristic:

  • Pipelining: all intermediate operations will return the stream object itself. In this way, multiple operations can be connected in series into a pipe
  • Internal iteration: Stream provides an internal iteration method, and the flow can directly call the traversal method.

Termination method

  • void forEach(Consumer<? super T> action); Receive and process each parameter
  • long count(); Number of statistical elements

Delay method

  • Stream<T> filter(Predicate<? super T> predicate); Convert a stream into a subset stream for filtering
  • <R> Stream<R> map(Function<? super T, ? extends R> mapper); Mapping can type convert all elements in a collection
  • Stream<T> skip(long n); Skip the first few
  • Stream<T> limit(long maxSize); Take the first few

Static method

  • static <T> Stream<T> concat(Stream<? extends T> a, Stream<? Extensions T > b): merge two streams together

Method of getting stream

  • Get stream from Collection: stream method
  • Get the stream according to the Map: get the Set set of key value pairs separately, and then call the stream method
  • Get stream from array: Stream.of (array object)

Sample code

public static void main(String[] args) {
        Integer[] ints={1,2,3,11,33,133,22,5};
        String[] strings={"Zhang San","Li Si","Wang Wu","Zhao Liu","Zhang Sanfeng"};
        Stream<Integer> ints1 = Stream.of(ints);//Get stream object
        Stream<String> strings1 = Stream.of(strings);//Get stream object
        //Use filters greater than 5 and less than 150 for final output
        Stream<Integer> stream1 = ints1.filter(i -> i > 5).filter(i -> i < 150);//ints1 has been used and cannot be used again
        //Name with surname Zhang and length greater than 2
        Stream<String> stream2 = strings1.filter(s -> s.startsWith("Zhang")).filter(s -> s.length() > 2);
        Stream<? extends Serializable> concat = Stream.concat(stream1, stream2);//Splice
        concat.forEach(i-> System.out.print(i+"\t"));//eleven 	 thirty-three 	 one hundred and thirty-three 	 twenty-two 	 Zhang Sanfeng
        Stream<Integer> ints2 = Stream.of(ints);//Get stream object
        System.out.println();
        //Skip the first two and take the first four
        ints2.skip(2).limit(4).forEach(i-> System.out.print(i+"\t"));//3	11	33	133
    }

Method reference

Method reference is a short form of Lambda expression

The parameter passed in Lambda must be the type that the method in the method reference can receive

 public static void main(String[] args) {
        //printstring("HELLO",s-> System.out.println(s));
        printstring("hello", System.out::println);
    }
System.out::println is equivalent to s - > system. Out. Println (s)

Specific use method

  • Using member methods with object names   Object name: member method name
  • Reference static methods by class name   Class name:: method name   For example: Math::abs
  • The member Method Super:: method name is referenced by super
  • Reference member method this:: method name through this
  • Constructor reference of class   Class name:: new, for example: name - > new person (name) is equivalent to Person::new
  • Constructor reference of array    Length - > New Int [length] is equivalent to int []: New

Sample code

//Method reference between parent and child (the subclass calls the method with functional interface and passes the parameters to the parent method (if there is no parameter, call the method of the parent class))
public class Zi extends Fu{
    @Override
    public void sey() {
        System.out.println("My subclass");
    }
    public void method(test1 t){
        t.printaaa();
    }
    public void show(){
        method(super::sey);//I'm my father
        method(this::sey);//My subclass
    }
}
//A reference to the constructor of the class
  private static person buind(String name,int age,builder builder){
        //The interface function obtains a parameter to establish an object
        return builder.test2(name,age);
    }
    public static void main(String[] args) {
        System.out.println( buind("Wang Huan",12,person::new).toString());//person{name = 'Wang Huan', age=12}
    }
//Reference to array constructor
private static int[] binder(int l,test3 test3){
        //The function of the interface is to receive a parameter (array length) to create an array object
        return test3.num(l);
    }
    public static void main(String[] args) {
        int[] binder = binder(12, int[]::new);
        System.out.println(binder.length);//12
    }

Tags: Java

Posted on Wed, 01 Sep 2021 16:01:10 -0400 by sarahk