java中Collectors.toMap() 方法使用技巧

java中Collectors.toMap() 方法使用技巧

Collectors.toMap() 方法是把List转Map的操作。

代码

	public static void main(String[] args) {
		List<Employee> employeeList = Arrays.asList(
				new Employee(101, "张三", 18, 9999.99),
				new Employee(102, "李四", 59, 6666.66),
				new Employee(103, "王五", 28, 3333.33),
				new Employee(104, "赵六", 8, 7777.77)
		);
		/**
		 * id作为Map的key,name作为value的集合
		 * */
		Map<Integer, String> collect1 = employeeList.stream().collect(Collectors.toMap(Employee::getId, Employee::getName));
		System.out.println(collect1);//{101=张三, 102=李四, 103=王五, 104=赵六}
		/**
		 * id作为map的集合,Employee对象作为Map的value
		 */
		Map<Integer, Employee> collect2 = employeeList.stream().collect(Collectors.toMap(Employee::getId, t -> t));
		System.out.println(collect2);//{101=Employee(id=101, name=张三, age=18, salary=9999.99), 102=Employee(id=102, name=李四, age=59, salary=6666.66), 103=Employee(id=103, name=王五, age=28, salary=3333.33), 104=Employee(id=104, name=赵六, age=8, salary=7777.77)}
		/**
		 * id作为map的集合,Employee对象作为Map的value
				*/
		Map<Integer, Employee> collect3 = employeeList.stream().collect(Collectors.toMap(Employee::getId, Function.identity()));
		System.out.println(collect3);//{101=Employee(id=101, name=张三, age=18, salary=9999.99), 102=Employee(id=102, name=李四, age=59, salary=6666.66), 103=Employee(id=103, name=王五, age=28, salary=3333.33), 104=Employee(id=104, name=赵六, age=8, salary=7777.77)}
	}

在上面代码中,map的ID不能重复,如果重复就会报错,下面我们引入个重载方法。

/**
		 * id作为Map的key,name作为value的集合。 如果ID相等,则返回第一个id的name
		 */
		Map<Integer, String> collect1 = employeeList.stream().collect(Collectors.toMap(Employee::getId, Employee::getName, (n1, n2) -> n1));
		System.out.println(collect1);
		/**
		 * id作为Map的key,name作为value的集合。 如果ID相等,则返回第一个id的Employee
		 */
		Map<Integer, Employee> collect2 = employeeList.stream().collect(Collectors.toMap(Employee::getId, t -> t, (n1, n2) -> n1));
		System.out.println(collect2);
		/**
		 * id作为Map的key,name作为value的集合。 如果ID相等,则返回第一个id的Employee
		 */
		Map<Integer, Employee> collect3 = employeeList.stream().collect(Collectors.toMap(Employee::getId, Function.identity(), (n1, n2) -> n1));
		System.out.println(collect3);
  • 21
    点赞
  • 79
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 6
    评论
Java 8Collectors.toMap方法可以将Stream的元素转换为一个Map对象。该方法有三个参数:keyMapper,valueMapper和mergeFunction。其,keyMapper用于将Stream的元素转换为Map的key,valueMapper用于将Stream的元素转换为Map的value,mergeFunction用于处理当key重复时的冲突情况。 下面是一个例子,假设我们有一个List对象,其包含多个Person对象,每个Person对象都有一个唯一的id属性和一个name属性。我们可以使用Collectors.toMap方法将List转换为一个以id为key,以Person对象为value的Map对象: ```java List<Person> personList = new ArrayList<>(); // 假设我们已经将多个Person对象添加到了personList Map<Integer, Person> personMap = personList.stream() .collect(Collectors.toMap(Person::getId, Function.identity())); ``` 在上面的例子,Person::getId表示将Person对象的id属性作为Map的key,Function.identity()表示将Person对象本身作为Map的value。如果我们想要将Person对象的name属性作为Map的value,可以这样写: ```java Map<Integer, String> personMap = personList.stream() .collect(Collectors.toMap(Person::getId, Person::getName)); ``` 如果我们的List有重复的id,那么上面的代码将会抛出IllegalStateException异常。为了避免这种情况,我们可以使用mergeFunction参数来处理冲突。例如,如果我们想要将重复的id的Person对象合并为一个List,可以这样写: ```java Map<Integer, List<Person>> personMap = personList.stream() .collect(Collectors.toMap(Person::getId, Collections::singletonList, (list1, list2) -> { List<Person> resultList = new ArrayList<>(list1); resultList.addAll(list2); return resultList; })); ``` 在上面的代码Collections::singletonList表示将Person对象转换为只包含一个元素的List,mergeFunction参数则表示将两个List合并为一个List。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

北漂IT民工_程序员_ZG

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值