将List分组转成Map是日常开发中常见的需求,我们就来总结一下常见的几种写法。
有如下代码:
@Data
class Person {
private String uuid;
private String name;
private String gender;
private int age;
public Person(String name, String gender, int age) {
this.uuid = UUID.randomUUID().toString();
this.name = name;
this.gender = gender;
this.age = age;
}
}
和一个List集合:
List<Person> persons = new ArrayList<>();
persons.add(new Person("张三", "男", 27));
persons.add(new Person("李四", "男", 14));
persons.add(new Person("王五", "女", 17));
persons.add(new Person("赵六", "女", 34));
1. partitioningBy 分两组
partitioningBy
要求传入一个Predicate
,会按照满足条件和不满足条件分成两组,得到的结果是Map<Boolean, List<T>>
结构,比如我们按是否未成年分成两组:
Map<Boolean, List<Person>> personsByAge = persons.stream()
.collect(Collectors.partitioningBy(p -> p.getAge() > 18));
System.out.println(JSON.toJSONString(personsByAge));
Output:
{
false: [{
"age": 14,
"gender": "男",
"name": "李四",
"uuid": "9fc3be98-f676-42a4-9f02-ebdab328103a"
}, {
"age": 17,
"gender": "女",
"name": "王五",
"uuid": "3621044d-25a1-4946-a765-57b074f63f26"
}],
true: [{
"age": 27,
"gender": "男",
"name": "张三",
"uuid": "3f87ec59-29a1-4137-b95b-ae755f0e06ca"
}, {
"age": 34,
"gender": "女",
"name": "赵六",
"uuid": "04ed8e9f-545b-49f5-a28b-ce0cccd15663"
}]
}
2. groupingBy 分多组
比如按照性别进行分组,得到的是Map<String, List<T>>
结构:
Map<String, List<Person>> personByGender = persons.stream()
.collect(Collectors.groupingBy(Person::getGender));
System.out.println(JSON.toJSONString(personByGender));
Output:
{
"女": [{
"age": 17,
"gender": "女",
"name": "王五",
"uuid": "feb8ca82-789f-445e-9e85-c14aa1d70546"
}, {
"age": 34,
"gender": "女",
"name": "赵六",
"uuid": "6402b5ec-03cd-45d1-aa6d-7134509ca670"
}],
"男": [{
"age": 27,
"gender": "男",
"name": "张三",
"uuid": "e2c5ec58-5767-4807-8470-56a016dbc5eb"
}, {
"age": 14,
"gender": "男",
"name": "李四",
"uuid": "d10aad57-038b-4ff8-8b36-86045d657c5a"
}]
}
3. toMap 自定义<key, value>
前面介绍的partitioningBy
和groupingBy
返回Map
的value
部分都是List<T>
结构的,有时我们需要value
是对象的一个属性,比如我们想构造一个uuid
到name
的映射,以方便通过uuid
快速获取人员的名字:
Map<String, String> uuidNameMap = persons.stream()
.collect(Collectors.toMap(Person::getUuid, Person::getName));
System.out.println(JSON.toJSONString(uuidNameMap));
Output:
{
"7a021022-fa62-4f57-bf33-873b8e030cc3": "王五",
"e0bad9e6-2c3c-417e-9d27-3b321312421a": "张三",
"895b0f95-b4fd-481e-ba6c-33f0b636e6cf": "李四",
"fcb6f403-8489-4853-98c5-6f41341165ba": "赵六"
}
实际情况有可能同一个key
会对应多个value
,就有可能抛Duplicate key
异常。这时可以传入第三个参数决定重复时如何选择,比如我们想构造<name, uuid>
的映射,但是考虑可能有重名的可能,就可以这么做:
Map<String, String> nameUuidMap = persons.stream()
.collect(Collectors.toMap(Person::getName, Person::getUuid, (p1, p2) -> p1));
System.out.println(JSON.toJSONString(nameUuidMap));
这里(p1, p2) -> p1
表示如果重复则取前者。