李林超博客
首页
归档
留言
友链
动态
关于
归档
留言
友链
动态
关于
首页
Java
正文
四、Stream流List和Map互转
Leefs
2021-07-28 PM
3184℃
0条
# 四、Stream流List和Map互转 ### 前言 本篇介绍Stream流List和Map互转,同时在转换过程中遇到的问题分析。 ### 一、Map转List #### 1.1 分析 ##### 按照默认顺序 ```java mapToList.entrySet().stream().map(a -> new User(a.getKey(), a.getValue())).collect(Collectors.toList()); ``` ##### 根据key排序 ```java mapToList.entrySet().stream().sorted(Comparator.comparing(a -> a.getKey())).map(a -> new User(a.getKey(),a.getValue())).collect(Collectors.toList()); ``` ##### 根据key排序 ```java mapToList.entrySet().stream().sorted(Map.Entry.comparingByKey()).map(a -> new User(a.getKey(),a.getValue())).collect(Collectors.toList()); ``` ##### **根据key倒序排序** ```java mapToList.entrySet().stream().sorted(Map.Entry.comparingByKey(Comparator.reverseOrder())).map(a -> new User(a.getKey(),a.getValue())).collect(Collectors.toList()); ``` ##### **根据value排序** ```java mapToList.entrySet().stream().sorted(Comparator.comparing(Map.Entry::getValue)).map(a -> new User(a.getKey(), a.getValue())).collect(Collectors.toList()); ``` ##### 根据value倒序排序 ```java mapToList.entrySet().stream().sorted(Collections.reverseOrder(Map.Entry.comparingByValue())).map(a -> new User(a.getKey(),a.getValue())).collect(Collectors.toList()); ``` #### 1.2 完整代码 ##### 实体类 ```java /** * @author lilinchao * @date 2021/7/28 * @description 1.0 **/ public class User { private Integer id; private String name; public User() { } public User(Integer id, String name) { this.id = id; this.name = name; } public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } @Override public String toString() { return "User{" + "id=" + id + ", name='" + name + '\'' + '}'; } } ``` ##### Map转List代码 ```java import java.util.*; import java.util.stream.Collectors; /** * @author lilinchao * @date 2021/7/28 * @description 1.0 **/ public class StreamMapToList { /** * 数据初始化 */ private static final Map
mapToList; static { mapToList = new HashMap
(); mapToList.put(1003, "Thymee"); mapToList.put(1001, "Leefs"); mapToList.put(1002, "Jeyoo"); } public static void main(String ages[]){ List
userList = defaultOrder(); System.out.println(userList); List
userList2and = orderByKeyMethodOne(); System.out.println(userList2and); List
userList3and = orderByKeyMethodTwo(); System.out.println(userList3and); List
userList4and = reverseOrderByKey(); System.out.println(userList4and); List
userList5and = orderByValue(); System.out.println(userList5and); List
userList6and = reverseOrderByValue(); System.out.println(userList6and); } /** * 按照默认顺序 */ public static List
defaultOrder(){ List
userList = mapToList.entrySet().stream().map(a -> new User(a.getKey(), a.getValue())).collect(Collectors.toList()); return userList; } /** *根据key排序,方法1 */ public static List
orderByKeyMethodOne(){ List
userList = mapToList.entrySet().stream().sorted(Comparator.comparing(a -> a.getKey())).map(a -> new User(a.getKey(),a.getValue())).collect(Collectors.toList()); return userList; } /** *根据key排序,方法2 */ public static List
orderByKeyMethodTwo(){ List
userList = mapToList.entrySet().stream().sorted(Map.Entry.comparingByKey()).map(a -> new User(a.getKey(),a.getValue())).collect(Collectors.toList()); return userList; } /** *根据key倒序排序 */ public static List
reverseOrderByKey(){ List
userList = mapToList.entrySet().stream().sorted(Map.Entry.comparingByKey(Comparator.reverseOrder())).map(a -> new User(a.getKey(),a.getValue())).collect(Collectors.toList()); return userList; } /** * 根据value排序 */ public static List
orderByValue(){ List
userList = mapToList.entrySet().stream().sorted(Comparator.comparing(Map.Entry::getValue)).map(a -> new User(a.getKey(), a.getValue())).collect(Collectors.toList()); return userList; } /** *根据value倒序排序 */ public static List
reverseOrderByValue(){ List
userList = mapToList.entrySet().stream().sorted(Collections.reverseOrder(Map.Entry.comparingByValue())).map(a -> new User(a.getKey(),a.getValue())).collect(Collectors.toList()); return userList; } } ``` ##### 运行结果 ```json [User{id=1001, name='Leefs'}, User{id=1002, name='Jeyoo'}, User{id=1003, name='Thymee'}] [User{id=1001, name='Leefs'}, User{id=1002, name='Jeyoo'}, User{id=1003, name='Thymee'}] [User{id=1001, name='Leefs'}, User{id=1002, name='Jeyoo'}, User{id=1003, name='Thymee'}] [User{id=1003, name='Thymee'}, User{id=1002, name='Jeyoo'}, User{id=1001, name='Leefs'}] [User{id=1002, name='Jeyoo'}, User{id=1001, name='Leefs'}, User{id=1003, name='Thymee'}] [User{id=1003, name='Thymee'}, User{id=1001, name='Leefs'}, User{id=1002, name='Jeyoo'}] ``` ### 二、List转Map #### 2.1 分析 ##### **指定key-value,value是对象中的某个属性值** ```java userList.stream().collect(Collectors.toMap(User::getId, User::getName)); ``` ##### 指定key-value,value是对象本身 > User->User 是一个返回本身的lambda表达式 ```java userList.stream().collect(Collectors.toMap(User::getId, User->User)); ``` ##### 指定key-value,value是对象本身 > Function.identity()是简洁写法,也是返回对象本身 ```java userList.stream().collect(Collectors.toMap(User::getId, Function.identity())); ``` ##### 指定key-value,key 冲突的解决办法 > (key1,key2)->key2:第二个key覆盖第一个key > (key1,key2)->key1:保留第一个key ```java userList.stream().collect(Collectors.toMap(User::getId, Function.identity(),(key1,key2)->key2)); ``` #### 2.2 完整代码 ```java import java.util.Arrays; import java.util.List; import java.util.Map; import java.util.function.Function; import java.util.stream.Collectors; /** * @author lilinchao * @date 2021/7/28 * @description 1.0 **/ public class StreamListToMap { private static final List
userList; static { userList = Arrays.asList( new User(1003,"keko"), new User(1001,"jeek"), new User(1002,"mack") ); } public static void main(String ages[]){ Map
map = method01(); System.out.println(map); Map
map2and = method02(); System.out.println(map2and); Map
map3and = method03(); System.out.println(map3and); Map
map4and = method04(); System.out.println(map4and); } /** * 指定key-value,value是对象中的某个属性值 */ public static Map
method01(){ Map
userMap = userList.stream().collect(Collectors.toMap(User::getId, User::getName)); return userMap; } /** *指定key-value,value是对象本身,User->User 是一个返回本身的lambda表达式 */ public static Map
method02(){ Map
userMap = userList.stream().collect(Collectors.toMap(User::getId, User->User)); return userMap; } /** * 指定key-value,value是对象本身,Function.identity()是简洁写法,也是返回对象本身 */ public static Map
method03(){ Map
userMap = userList.stream().collect(Collectors.toMap(User::getId, Function.identity())); return userMap; } /** * 指定key-value,key 冲突的解决办法 * (key1,key2)->key2:第二个key覆盖第一个key * (key1,key2)->key1:保留第一个key */ public static Map
method04(){ Map
userMap = userList.stream().collect(Collectors.toMap(User::getId, Function.identity(),(key1,key2)->key2)); return userMap; } } ``` ##### 运行结果 ```json {1001=jeek, 1002=mack, 1003=keko} {1001=User{id=1001, name='jeek'}, 1002=User{id=1002, name='mack'}, 1003=User{id=1003, name='keko'}} {1001=User{id=1001, name='jeek'}, 1002=User{id=1002, name='mack'}, 1003=User{id=1003, name='keko'}} {1001=User{id=1001, name='jeek'}, 1002=User{id=1002, name='mack'}, 1003=User{id=1003, name='keko'}} ``` ### 三、List转Map常见问题 #### 3.1 常见问题 ##### 问题一 报错`Duplicate key xxxx` > 该问题是因为在生成Map集合时key值重复造成的 ##### 解决方案 **1. 后面的value覆盖前面的value** ```java userList.stream().collect(Collectors.toMap(User::getId, User::getName, (key1, key2) -> key2)); ``` 也可以保留前面的value,将key2换成key1即可 **2. value进行拼接** ```java userList.stream().collect(Collectors.toMap(User::getId, User::getName, (key1, key2) -> key1+","+key2)); ``` **3. key重复时,返回集合** ```java userList2and.stream().collect(Collectors.toMap(User::getId, p -> { List
getNameList = new ArrayList<>(); getNameList.add(p.getName()); return getNameList; }, (List
value1, List
value2) -> { value1.addAll(value2); return value1; } )); ``` ##### 问题二 报错:`Exception in thread "main" java.lang.NullPointerException` > 该问题是因为在存入Map集合时value值为null ##### 解决方案 在转换流中加上判空,即便value为空,依旧输出。(与上面方法三相同) #### 3.2 完整代码 ```java import java.util.ArrayList; import java.util.Arrays; import java.util.List; import java.util.Map; import java.util.stream.Collectors; /** * @author lilinchao * @date 2021/7/28 * @description 1.0 **/ public class StreamListToMapAnalyze { private static final List
userList; private static final List
userList2and; static { //重复的key userList = Arrays.asList( new User(1003,"keko"), new User(1001,"jeek"), new User(1001,"teek"), new User(1002,"mack") ); //重复的key,value为null userList2and = Arrays.asList( new User(1003,"keko"), new User(1001,"jeek"), new User(1001,"teek"), new User(1002,null) ); } public static void main(String ages[]){ Map
map = keyRepeatMethod01(); System.out.println(map); Map
map2and = keyRepeatMethod02(); System.out.println(map2and); Map
> map3and = keyRepeatMethod03(); System.out.println(map3and); } /** * key重复时,后面的value覆盖前面的value */ public static Map
keyRepeatMethod01(){ Map
userMap = userList.stream().collect(Collectors.toMap(User::getId, User::getName, (key1, key2) -> key2)); return userMap; } /** * key重复时,value进行拼接 */ public static Map
keyRepeatMethod02(){ Map
userMap = userList.stream().collect(Collectors.toMap(User::getId, User::getName, (key1, key2) -> key1+","+key2)); return userMap; } /** * key重复时,返回集合 * value值为空,在转换流中加上判空,即便value为空,依旧输出 */ public static Map
> keyRepeatMethod03(){ Map
> userListMap = userList2and.stream().collect(Collectors.toMap(User::getId, p -> { List
getNameList = new ArrayList<>(); getNameList.add(p.getName()); return getNameList; }, (List
value1, List
value2) -> { value1.addAll(value2); return value1; } )); return userListMap; } } ``` ##### 运行结果 ```json {1001=teek, 1002=mack, 1003=keko} {1001=jeek,teek, 1002=mack, 1003=keko} {1001=[jeek, teek], 1002=[null], 1003=[keko]} ``` *附:参考文章链接* *https://www.cnblogs.com/fugitive/p/13938020.html* *https://www.jb51.net/article/170365.htm*
标签:
Stream流
非特殊说明,本博所有文章均为博主原创。
如若转载,请注明出处:
https://lilinchao.com/archives/1376.html
上一篇
MySQL按照日期统计报表
下一篇
Git概述
取消回复
评论啦~
提交评论
栏目分类
随笔
2
Java
326
大数据
229
工具
31
其它
25
GO
48
NLP
8
标签云
机器学习
Hbase
JavaWeb
SQL练习题
Spark SQL
MySQL
Kibana
数据结构
Typora
CentOS
查找
gorm
ajax
JavaWEB项目搭建
Elasticsearch
Java编程思想
Spark Streaming
Elastisearch
Azkaban
递归
Java阻塞队列
GET和POST
Spark Core
JavaScript
Flume
Golang基础
RSA加解密
ClickHouse
Thymeleaf
Quartz
友情链接
申请
范明明
庄严博客
Mx
陶小桃Blog
虫洞