Stream ToMap(Collectors.toMap) 实践

RequirementsList TO MapList Stream 转换 Map时向collect()方法中传递Collector对象,对象由Collectors.toMap()方法返回。如下实现List转换为MapList<GroupBrandCateBO> list = new ArrayList<>( Arrays.asList( ...
摘要由CSDN通过智能技术生成

Requirements

List TO Map
List Stream 转换 Map时向collect()方法中传递Collector对象,对象由Collectors.toMap()方法返回。

如下实现List转换为Map

List<GroupBrandCateBO> list = new ArrayList<>(
      Arrays.asList(
              new GroupBrandCateBO("v1", "g1", "b1"),
              new GroupBrandCateBO("v1", "g1", "b1"),
              new GroupBrandCateBO("v3", "g3", "b3")
      )
);
Map<String, String> map = list.stream().collect(Collectors.toMap(item -> item.getVersion(), item -> item.getGroupCode(), (oldVal, currVal) -> oldVal, LinkedHashMap::new)); 
System.out.println(map.getClass());
Map<String, String> map0 = list.stream().collect(Collectors.toMap(item -> item.getVersion(), item -> item.getGroupCode(), (oldVal, currVal) -> oldVal));
System.out.println(map0.getClass());
System.out.println(ma
  • 11
    点赞
  • 68
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值