-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCollectorsDemo.java
More file actions
42 lines (36 loc) · 1.2 KB
/
Copy pathCollectorsDemo.java
File metadata and controls
42 lines (36 loc) · 1.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
package FDynamic.Stream;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
public class CollectorsDemo {
public static void main(String[] args) {
List<Person> personList= Arrays.asList(
new Person("aaa", 18, new BigDecimal(123)),
new Person("aaa", 18, new BigDecimal(123)),
new Person("bbb", 19, new BigDecimal(124))
);
groupBy(personList);
partionBy(personList);
}
/**
* 根据人员的name来进行分组
* @param personList
*/
public static void groupBy(List<Person> personList){
Map<String, List<Person>> map=personList.stream()
.collect(Collectors.groupingBy(Person::getName));
System.out.println(map);
}
/**
* 根据人员的name是否是aaa来进行区分
* @param personList
*/
public static void partionBy(List<Person> personList){
Map<Boolean, List<Person>> map=personList.stream()
.collect(Collectors.partitioningBy(person -> "aaa".equals(person.getName())));
System.out.println(map);
}
}