preface

Unlike Java7’s straightforward segmented and verbose description, Java8’s comprehensive use of functional interfaces, lambda expressions, and streams makes business logic clear and coherent in one step. Especially suitable for data flow processing (e.g. filtering, transformation, grouping, statistics, etc.).

Lambda expressions and function interfaces

1.1 Lambda Expressions

A List < Integer > List = Arrays. AsList (1,2,3,4,5,6,7,8,9,10); List. ForEach (s -> system.out.println (s)); List.foreach (s -> {system.out.println (s)});Copy the code

Intuitively, a lambda expression is the complete implementation of a functional interface. It consists of two parts, the input parameter and the business logic. The input parameter is item, and the implementation logic of the method is System.out.println(item).

1.2 Functional interfaces

A functional interface is one that has one and only abstract method (methods in the Object class are not included)

Consumer

@FunctionalInterface public interface Consumer<T> { void accept(T t); . }Copy the code

Supplier

@FunctionalInterface
public interface Supplier<T> {
    T get();
}
Copy the code

Predicate

@FunctionalInterface
public interface Predicate<T> {
    boolean test(T t);
}
Copy the code

Function

@FunctionalInterface
public interface Function<T, R> {
    R apply(T t);
}
Copy the code

1.3 Use of lambda expressions in conjunction with functional interfaces

  • Example implementation of a lambda expression for the functional interface Consumer
Consumer<String> consumer = (s) -> System.out.println(s);
Copy the code
  • Example use of lambda expressions
A List < Integer > List = Arrays. AsList (1,2,3,4,5,6,7,8,9,10); List. ForEach (consumer); List.foreach ((s) -> system.out.println (s));Copy the code

Item is the input to the accept() method of the Consumer interface, and System.out.println(item) is the implementation logic of the Accept function.

  • summary

Here, the lambda expression can be used as the input parameter of the function, in fact, because the lambda expression itself is an object, which is equivalent to an object reference to the function. The second reason why you can ignore the input parameter type, that is, why you can just pass an expression, is because the parameter definition of a function is the defined functional interface (for example, Consumer).

1.4 Use of lambda expressions in forEach

  • The forEach method is the default method in the Iterable interface
default void forEach(Consumer<? super T> action) { Objects.requireNonNull(action); for (T t : this) { action.accept(t); }}Copy the code

The forEach method takes a reference to a Consumer (functional interface) object as an input parameter and calls the Accept () method of Consumer in the method, executing the business logic of the lambda expression.

  • The list collection calls forEach to execute a lambda expression that prints all the elements in the collection
Public class ForEachTest {@test public void test_Consumer() {List<Integer> List = array.aslist (1,2,3,4,5,6,7,8,9,10);  list.forEach( item -> System.out.println(item) ); }}Copy the code

The List interface inherits from the Collection interface, which in turn inherits from the Iterable interface, so the List can call the forEach method.

2 the stream flow

Grouping groupingBy

GroupingBy Follow-up Procedure Example 1

@Data public class StreamDemo { @Data class User{ String name; String cityCode; } public void main(String[] args) { List<User> list = new ArrayList<>(); // Grouped according to their city, And translate the Chinese name of the corresponding city Map<String for the list of grouped people. List<User>> map = list.parallelStream().collect(Collectors.groupingBy( User::getCityCode, Collectors.mapping( item -> { User user = new User(); // Translate the Chinese name //... return user; }, Collectors.toList() ) )); }}Copy the code

GroupingBy Follow-up Procedure Example 2

@Data public class StreamDemo { @Data class User{ String name; String cityCode; } public void main(String[] args) { List<User> list = new ArrayList<>(); // Grouped according to their city, And count the total number of Collectors in each group Map<String, Long> Map = list.parallelstream ().collect(Collectors. GroupingBy (User::getCityCode, Collectors.counting() )); }}Copy the code

Serial and concurrent

Quality than

3 DateTime

Examples of DateTime usage

Traditional Date tools

Timestamp conversion in Excel

Timestamp conversion in SQL such as Oracle