Lambda common methods


This article briefly summarizes the common methods in Lambda

For students unfamiliar with Lambda, please refer to this article for introduction Understanding of Lambda , Lambda is really more comfortable

If you are new to Lambda, but are not familiar with Consumer, Supplier, Predicate and Function, please refer to this article Quick understanding of Consumer, Supplier, Predicate and Function

If you are already familiar with Lambda, but you don't understand its execution principle, please refer to my article Lambda execution principle

Let's start with an example

Here are five students. Put them into the group

       class Student {
           private String name;
           private Integer age;

        Student a = new Student("a", 20);
        Student b = new Student("b", 18);
        Student c = new Student("c", 22);
        Student d = new Student("d", 19);
        Student e = new Student("e", 18);
        List<Student> list = new ArrayList<>();

In daily development, there may be the following requirements:

1, Traverse the set


forEach accepts a Consumer expression

2, Take out the student's name

        List<String> nameList =

Stream can transform one form of stream into another. The code above is to transform the stream containing the whole student object into a stream containing names separately. Finally, collect the stream.

3, Take out students older than 19

        List<Student> studentList =
                .filter(s -> s.getAge() > 19)

The filter receives a Predicate expression and discards the student objects that do not match the expression in the stream.

4, Take out the age of students without repetition

        List<Integer> ageDistinctList =

Just like mysql's de duplication keyword, it uses distinct

5, Full match, any match, none match

Requirement 1: whether all students are over the age of 18 (full match)

        boolean flag =
                .allMatch(i -> i > 18);

Requirement 2: whether there are students older than 21 (any matching)

        boolean flag =
                .anyMatch(i -> i > 21);

Requirement 3: whether all students are no older than 22 (no match)

        boolean flag =
                .noneMatch(i -> i > 22);

6, Sort

Needs, sorted by age of student


The default is to output in ascending order according to age, or in descending order, as long as.reversed is added, as shown in the following figure

If you sort by age and then by name, you can write:

7, Pagination

Requirement: the current page is page 2, with 3 items per page
                .skip((2 - 1) * 3) //(current page - 1) * number of items displayed per page
                .limit(3)          //Number of items per page

First, sort by age, then skip the data on the previous page and take out the limit data.

8, Grouping

Needs: grouped by age

        Map<Integer, List<Student>> group =;
        group.forEach((k, v) -> System.out.println("k:" + k + ",v:" + v.toString()));


9, String splicing

Requirement: output comma separated string of student name

        String str =","));


10, Mathematical operation

Demand 1: find the total age of students

        int sum =;

Demand 2: average age of students

        double average =

Note that average() returns an OptionalDouble, and in order to prevent the collection from having no elements, we must use orElse to specify a default value.

Similarly, max is used to get the maximum value, min is used to get the minimum value, and the default value needs to be specified.

Continue to add cases





Keywords: Lambda MySQL

Added by Jazzy Girl on Wed, 10 Jun 2020 05:40:45 +0300