亲宝软件园·资讯

展开

Java  Lambda转 Map

放羊的牧码 人气:0

故事背景

我们平时在项目中经常会遇到 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));

方式一(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));
 
// 输出
{
	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"
	}]
}

方式二(groupingBy 分多组)

比如按照性别进行分组,得到的是Map<String, List<T>>结构

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"
	}]
}

方式三(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));
 
// 输出
{
	"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>的映射,但是考虑可能有重名的可能,就可以这么做(Tips:这里(p1, p2) -> p1表示如果重复则取前者)

Map<String, String> nameUuidMap = persons.stream()
	.collect(Collectors.toMap(Person::getName, Person::getUuid, (p1, p2) -> p1));
System.out.println(JSON.toJSONString(nameUuidMap));

附加: 如何呈现 <Person::getName, Person> 这种数据结构呢?

Map<String, Person> namePersonMap = persons.stream()
	.collect(Collectors.toMap(Person::getName, v -> v, (p1, p2) -> p1));
System.out.println(JSON.toJSONString(namePersonMap));

加载全部内容

相关教程
猜你喜欢
用户评论