Stream.toMap_mb5fdb0fbba4f73

  Collectors类的tomap方法将流收集到映射实例中。

list 转 map

collection.stream().collect(Collectors.toMap(User::getId, User::getName));

解决Key冲突

collection.stream().collect(Collectors.toMap(User::getAge, Function.identity(),
(oldVal, newVal) -> newVal));

返回其他Map

collection.stream().collect(Collectors.toMap(User::getAge, Function.identity(), (oldVal, newVal) -> newVal, ConcurrentHashMap::new));

返回TreeMap

collection.stream() 
.sorted(Comparator.comparing(User::getName))
.collect(Collectors.toMap(User::getName, Function.identity(), (oldVal, newVal) -> newVal, TreeMap::new));

本站由小牛团队全力维护,小牛十年了,大家已经步入中年 。本站源码全部经过团队成员测试并调试,价格可能比其它网站略贵几元钱,不解释!
小牛资源 » Stream.toMap_mb5fdb0fbba4f73

发表评论

全站资源亲测可用,价格略高几元,不解释

立即查看 了解详情