List转Map处理
作者:互联网
- List对象装一个Map<String,String>
在Java8中新增了stream流的操作,对于代码书写更为简便,而且更容易看的懂
List<Unit> unitList = UnitMapper.selectList(queryWrapper); Map<String, String> map = unitList.stream().collect(Collectors.toMap(Unit::getStrGuid, Unit::getStrUnit));
- List对象转一个Map<String,Object>
List<Unit> unitList = UnitMapper.selectList(queryWrapper); unitList.stream().collect(Collectors.toMap(Unit::getStrGuid, Function.identity()));
Function.identity()返回一个跟输入一样的对象,等价于t -> t , 直接放在map的value
List<Unit> unitList = UnitMapper.selectList(queryWrapper);
unitList.stream().collect(Collectors.toMap(Unit::getStrGuid, Unit ->
Unit));
这两种效果就是一样的了.
标签:Map,toMap,unitList,stream,处理,List,Unit 来源: https://www.cnblogs.com/reverseapplepie/p/14870659.html