赞
踩
我们平时在项目中经常会遇到 List 转 Map 的情况,但是传统的方式又显得太臃肿,于是就想到 Lambda 神器,今天我们就来看看都有哪几种转换方式(List -> Map)
- // Person 实体类
- @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));
- Map<Boolean, List<Person>> personsByAge = persons.stream()
- .collect(Collectors.partitioningBy(p -> p.getAge() > 18));
- System.out.println(JSON.toJSONString(personsByAge));
-
- // 输出
- {
- 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"
- }]
- }
- Map<String, List<Person>> personByGender = persons.stream()
- .collect(Collectors.groupingBy(Person::getGender));
- System.out.println(JSON.toJSONString(personByGender));
-
- // 输出
- {
- "女": [{
- "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"
- }]
- }
- Map<String, String> uuidNameMap = persons.stream()
- .collect(Collectors.toMap(Person::getUuid, Person::getName));
- System.out.println(JSON.toJSONString(uuidNameMap));
-
- // 输出
- {
- "7a021022-fa62-4f57-bf33-873b8e030cc3": "王五",
- "e0bad9e6-2c3c-417e-9d27-3b321312421a": "张三",
- "895b0f95-b4fd-481e-ba6c-33f0b636e6cf": "李四",
- "fcb6f403-8489-4853-98c5-6f41341165ba": "赵六"
- }
- Map<String, String> nameUuidMap = persons.stream()
- .collect(Collectors.toMap(Person::getName, Person::getUuid, (p1, p2) -> p1));
- System.out.println(JSON.toJSONString(nameUuidMap));
- Map<String, Person> namePersonMap = persons.stream()
- .collect(Collectors.toMap(Person::getName, v -> v, (p1, p2) -> p1));
- System.out.println(JSON.toJSONString(namePersonMap));
- Map<String, Person> namePersonMap = persons.stream()
- .collect(Collectors.toMap(Person::getName, Function.identity()));
- System.out.println(JSON.toJSONString(namePersonMap));
- Map<String, Person> namePersonMap = persons.stream()
- .collect(Collectors.toMap(person -> person.getName(), Function.identity()));
- System.out.println(JSON.toJSONString(namePersonMap));
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。