It's jdk14, can't you use lambda expression?

1, Functional interface

1. Functional interface definition

   before learning lambda expressions, let's look at the functional interface:

  • Interface with only one abstract method
  • You can use @ FunctionalInterface annotation to declare and check functional interfaces
  • java.util.function The package defines a wealth of functional interfaces

2. Four functional interfaces of Java

Functional interface Parameter type Return type purpose
Consumer consumer interface T void Receive the object of type T for operation, method: void accept (t t t)
Supplier supply interface nothing T Returns an object of type T by T get()
Function < T, R > function interface T R Receives an object of type T for operation and returns an object of type R. Method: R apply (t t t)
Predicate predicate predicate interface T boolean Determines whether an object of type T satisfies a constraint and returns a boolean value. Method: boolean test (t t t)

2, lambda expression

1. What is lambda

  • lambda expression is a very important feature introduced by jdk8
  • Lambda is an anonymous function, which can be completed by lambda expression if it was expressed by an anonymous implementation class
  • Lambda is based on functional interface. Lambda is an instance of functional interface. If you use lambda expression to create the interface object, the interface must be functional interface
  • Lambda expression introduces new syntax and operators: '() - > {}', '- >' operator specifies the parameter list of lambda expression, that is, the parameter list of abstract method, and specifies the method body of lambda, that is, the implementation code logic of abstract method on the right

2. lambda syntax features (PS: rookie tutorial copy)

  • Optional type declaration: it is not necessary to declare parameter types, and the compiler can identify parameter values uniformly.
  • Optional parameter parentheses: one parameter does not need to define parentheses, but multiple parameters need to define parentheses.
  • Optional braces: if the body contains a statement, you do not need to use braces.
  • Optional return key: if the body has only one expression return value, the compiler will automatically return the value. The brace needs to specify that the expression returns a value.


  • 1. Generate 10 random numbers using the supply interface
* Generate 10 random numbers
public void testSupplier() {
    System.out.println("============lambda realization=============");
    Supplier<List<Integer>> listSupplier = () -> {
        List<Integer> list = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            list.add((int) (Math.random() * 100));
        return list;
    ============lambda Expression implementation=============
    [23, 69, 38, 69, 20, 60, 7, 83, 67, 82]
  • 2. Filter for matching strings using assertive interfaces
public void testPredicate() {
    String[] str = {"James", "Lucy", "Irving", "kevin", "pdd"};
    List<String> list = Arrays.asList(str);
    List<String> list1 = filterList(list, s -> s.contains("e"));
    List<String> list2 = filterList(list, s -> (s.length() > 5));

public List<String> filterList(List<String> list, Predicate<String> pre) {
    List<String> filterList = new ArrayList<>();
    for (String str : list) {
        if (pre.test(str)) {
    return filterList;
    [James, kevin]
  • 3, Rewrite comparator
public void test() {
    Set<Person> set = new TreeSet<>((o1, o2) -> o1.getId().compareTo(o2.getId()));
    set.add(new Person(1, "pdd"));
    set.add(new Person(4, "dgq"));
    set.add(new Person(3, "lbw"));
    set.add(new Person(2, "dsm"));
    System.out.println("according to id After sorting:" + set);

class Person {
    private Integer id;
    private String name;

    After sorting by ID: [Person(id=1, name=pdd), Person(id=2, name=dsm), Person(id=3, name=lbw), Person(id=4, name=dgq)]]

Keywords: Lambda Java

Added by udendra on Thu, 11 Jun 2020 08:00:03 +0300