New features of Java 8 -- lambda expression and Stream class

Posted by noodle on Sun, 06 Mar 2022 11:34:37 +0100

New features of Java 8 -- lambda expression and Stream class

1, lambda expression

Lambda expression itself is the implementation of an interface.
Lambda expression is a new feature of JDK8. It can replace the anonymous inner class of the interface and write more elegant Java code (equivalent to syntax sugar).
If you want to use lambda to simplify the anonymous inner class of the interface, you need the interface to be a functional interface

  • An interface with only one abstract method that must be implemented is called a Functional Interface
  • The functional interface has a special annotation mark written on the interface declaration, which is called * * @ functional interface**
    Format: (argument) - > {body}
    Writing rules
  • The type of parameter can be explicitly declared or inferred from the context. For example: (int a) has the same effect as (a)
  • All parameters shall be enclosed in parentheses and separated by commas. For example: (a, b) or (int a, String b)
  • Parentheses () can be omitted when there is only one parameter and its type can be deduced. For example: a - > return a * a
  • The body of a Lambda expression can contain zero or more statements
    If the body of a Lambda expression has only one statement, curly braces {} can be omitted. The return type of anonymous function is consistent with the body expression
  • If the body of a Lambda expression contains more than one statement, the expression must be contained in curly braces {} (forming a code block). The return type of the anonymous function is the same as that of the code block. If there is no return, it is null.
    There are four built-in interfaces in Java:
  1. Consumer < T > con consumer interface: void accept (T); Receive a parameter and "consume" it
  2. Supplier < T > sup supply interface: T get(); No parameters are accepted, only the return value, "Lei Feng"
  3. Function < T, R > fun functional interface: R apply (T); Receive a parameter and return another type of data after processing
  4. Predicate < T >: predicate interface: Boolean test (T); Receive a parameter and return a Boolean value for filtering

Use of Consumer

public class Test1 {
    public static void main(String[] args) {
    //lambda expressions essentially implement abstract methods in interfaces
        makeMoney(1000,num -> System.out.println("This lazy consumer"+num+"element"));
    }
    public static void makeMoney(double num, Consumer<Double>con){
        con.accept(num);
    }
}

Use of Supplier

public class Test1 {
    public static void main(String[] args) {
        final ArrayList<Integer> list = new Test1().getNumList(10, () -> new Random().nextInt(100));
        list.forEach(System.out::println);
    }
    //Write a method to pass the interface to be implemented into the parameters. In actual use, use lambda expression to implement the interface
    public ArrayList<Integer> getNumList(int num, Supplier<Integer> sup){
        final ArrayList<Integer> list = new ArrayList<>();
        for (int i = 0; i < num; i++) {
            final Integer e = sup.get();
            list.add(e);
        }
        return list;
    }
}

Use of Function

public class Test1 {
    public static void main(String[] args) {
        //With only one parameter, the compiler can easily infer that it is processing it, so it can write the method name directly
        System.out.println(new Test1().stringHander("haHHjfsjk", String::toUpperCase));
        System.out.println(new Test1().stringHander("  hfa    ", String::trim));
    }
    public String stringHander(String str,Function<String,String>fun){
        return fun.apply(str);
    }
}

Use of Predicate

public class Test1 {
    public static void main(String[] args) {
        final List<Integer> list = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16);
        final ArrayList<Integer> integers = new Test1().selectEven(list, x -> x % 2 == 0);
        integers.forEach(System.out::println);
    }

    public ArrayList<Integer> selectEven(List<Integer> list, Predicate<Integer>pre){
        final ArrayList<Integer> list1 = new ArrayList<>();
        for (Integer integer : list) {
            if(pre.test(integer)) list1.add(integer);
        }
        return list1;
    }
}

2, Stream class

Stream regards the set of elements to be processed as a stream. In the process of streaming, the Stream API is used to operate the elements in the stream, such as filtering, sorting, aggregation, etc.

  1. stream does not store data, but calculates the data according to specific rules, and generally outputs the results.
  2. stream does not change the data source and usually produces a new set or a value.
  3. stream has the characteristic of delayed execution, and the intermediate operation will be executed only when the terminal operation is called.
    Common creation methods
    1. Through Java util. Collection. The stream () method creates a stream from a collection
List<String> list = Arrays.asList("a", "b", "c");
// Create a sequential flow
Stream<String> stream = list.stream();
// Create a parallel stream
Stream<String> parallelStream = list.parallelStream();

2. Use * * Java util. Arrays. The stream (t [] array) * * method creates a stream with an array

int[] array={1,3,5,6,8};
IntStream stream = Arrays.stream(array);

3. Static methods using Stream: of(), iterate(), generate()

Stream<Integer> stream = Stream.of(1, 2, 3, 4, 5, 6);

Stream<Integer> stream2 = Stream.iterate(0, (x) -> x + 3).limit(4);
stream2.forEach(System.out::println);

Stream<Double> stream3 = Stream.generate(Math::random).limit(3);
stream3.forEach(System.out::println);

Stream is a sequential stream, in which the main thread performs operations in sequence, while parallel stream is a parallel stream.
The Optional class is a null able container object. If the value exists, the isPresent() method will return true, and calling the get() method will return the object.
Stream also supports traversal and matching elements of similar collections, but the elements in stream exist as Optional type. The traversal and matching of stream are very simple.
3.1 foreach/find/match
3.2 filter returns true or false
3.3 polymerization (max/min/count)
3.4 mapping (map/flatMap) mapping one flow to another
3.5 reduce
3.6 collect collects the elements in the stream and returns a set or array
3.6.2 Statistics (count/averaging)
3.6.3 grouping (partitioningBy/groupingBy)
3.7 sorted
3.8 extraction / combination

Topics: Java Eclipse RESTful