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