For example, the current business needs to group the obtained data, and then use another method to process the data.
The methods used are Collectors.groupingBy, Collectors.counting(), Collectors.reducing().
Then, take the following example as an example to collect the obtained lists and group the obtained data by gender, with gender as K and the number of genders as v.
Code example:
package Lx; import java.util.ArrayList; import java.util.List; import java.util.Map; import java.util.stream.Collectors; public class StreamLiu { public static void main(String[] args) { // Suppose there are three lists containing objects List<Person> list1 = new ArrayList<>(); Person person = new Person(); person.setIdCard("100001"); person.setName("Zhang San"); person.setAge("M"); list1.add(person); //Add some Person objects to list1 List<Person> list2 = new ArrayList<>(); //Add some Person objects to list2 Person person2 = new Person(); person2.setIdCard("100002"); person2.setName("李思"); person2.setAge("M"); list2.add(person2); List<Person> list3 = new ArrayList<>(); //Add some Person objects to list3 Person person3 = new Person(); person3.setIdCard("100003"); person3.setName("Zhao Fang"); person3.setAge("W"); list3.add(person3); // Merge these three linked lists into a total linked list List<Person> totalList = new ArrayList<>(); totalList.addAll(list1); totalList.addAll(list2); totalList.addAll(list3); //Requirement: Convert totalList into a collection with gender as the main key and the number of genders as value /***Method 1--start***/ Map<String, Integer> result = totalList.stream().collect(Collectors.groupingBy( Person::getAge, Collectors.reducing(0, e -> 1, Integer::sum) )); System.out.println("reult " + result); /***Method 2--start***/ Map<String, Long> result2 = totalList.stream().collect(Collectors.groupingBy( Person::getAge, Collectors.counting() )); System.out.println("reult " + result); } } class Person { private String idCard; // Other properties and methods private String name; //Gender 1 male 2 female private String age; public String getIdCard() { return idCard; } public void setIdCard(String idCard) { this.idCard = idCard; } //Other getter and setter methods public String getAge() { return age; } public void setAge(String age) { this.age = age; } public String getName() { return name; } public void setName(String name) { this.name = name; } @Override public String toString() { return "Person{" + "idCard='" + idCard + '\'' + //Other properties '}'; } }
Print results:
Key code analysis:
Method 1:
/***Method 1--start***/ Map<String, Integer> result = totalList.stream().collect(Collectors.groupingBy( Person::getAge, Collectors.reducing(0, e -> 1, Integer::sum) ));
This code uses the Stream API in Java 8 to group the objects in a `List` according to the `age` attribute of the `Person` object, calculates the number of elements in each group, and stores the result in a `Map Return in `. Specifically, the `Collectors.groupingBy` method and the `Collectors.reducing` method are used in the code, which represent grouping and reduction (calculation) operations on the list respectively.
The following is a detailed analysis:
1. `totalList.stream()`: Convert `totalList` to `Stream` object for operation using Stream API.
2. `Collectors.groupingBy(Person::getAge, Collectors.reducing(0, e -> 1, Integer::sum))`: Group the elements in `totalList` according to the `age` attribute of the `Person` object , and count the number of elements in each group.
* `Person::getAge` specifies grouping according to the `age` attribute of the `Person` object, that is, elements with the same `age` attribute are grouped into the same group.
* The `Collectors.reducing` method specifies how the elements in each group are calculated. In this example, the elements in each group are reduced (calculated) to the sum of their quantities. Specifically, in `Collectors.reducing(0, e -> 1, Integer::sum)`:
* `0` represents the initial value, that is, the initial value of the number of elements in each group is 0.
* `e -> 1` is a Lambda expression that maps each element to `1`, that is, counts each element.
* `Integer::sum` specifies the operation of adding all elements mapped to `1`, that is, adding the number of elements in each group.
Through such a reduction operation, the number of elements in each group is calculated.
3. Since the return value of the `Collectors.groupingBy` method is of type `Map
Method 2: (Because the value of method 2 is Long type, you can use Collectors.counting() directly)
/***Method 2--start***/ Map<String, Long> result2 = totalList.stream().collect(Collectors.groupingBy( Person::getAge, Collectors.counting() ));
This code uses the Stream API in Java 8 to group the objects in a `List` according to the `age` attribute of the `Person` object, calculates the number of elements in each group, and stores the result in a `Map Return in `. Specifically, the `Collectors.groupingBy` method and `Collectors.counting` method are used in the code, which represent grouping and counting operations on the list respectively.
The following is a detailed analysis:
1. `totalList.stream()`: Convert `totalList` to `Stream` object for operation using Stream API.
2. `Collectors.groupingBy(Person::getAge, Collectors.counting())`: Group the elements in `totalList` according to the `age` attribute of the `Person` object, and calculate the number of elements in each group .
* `Person::getAge` specifies grouping according to the `age` attribute of the `Person` object, that is, elements with the same `age` attribute are grouped into the same group.
* The `Collectors.counting()` method is used to count the number of elements in each group.
Through this operation, the number of elements in each group is calculated.
3. Since the return value of the `Collectors.groupingBy` method is of type `Map