本文来说下Stream实现List和Map互转总结
文章目录
实体类
本篇介绍Stream流List和Map互转,同时在转换过程中遇到的问题分析。
java
package cn.wideth.collect;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@NoArgsConstructor
@AllArgsConstructor
public class User {
private Integer id;
private String name;
}
Map转List代码
Map转List代码
java
package cn.wideth.collect;
import lombok.extern.slf4j.Slf4j;
import java.util.*;
import java.util.stream.Collectors;
@Slf4j
public class StreamMapToList {
/**
* 数据初始化
*/
private static final Map<Integer, String> mapToList;
static
{
mapToList = new HashMap<Integer, String>();
mapToList.put(10088, "ccc");
mapToList.put(10086, "aaa");
mapToList.put(10087, "bbb");
}
public static void main(String[] args) {
List<User> userList = defaultOrder();
System.out.println(userList);
List<User> userList2and = orderByKeyMethodOne();
System.out.println(userList2and);
List<User> userList3and = orderByKeyMethodTwo();
System.out.println(userList3and);
List<User> userList4and = reverseOrderByKey();
System.out.println(userList4and);
List<User> userList5and = orderByValue();
System.out.println(userList5and);
List<User> userList6and = reverseOrderByValue();
System.out.println(userList6and);
}
/**
*根据value倒序排序
*/
public static List<User> reverseOrderByValue(){
List<User> userList = mapToList.entrySet().stream().sorted(Collections.reverseOrder(Map.Entry.comparingByValue())).
map(a -> new User(a.getKey(),a.getValue())).collect(Collectors.toList());
return userList;
}
/**
* 根据value排序
*/
public static List<User> orderByValue(){
List<User> userList = mapToList.entrySet().stream().sorted(Comparator.comparing(Map.Entry::getValue)).
map(a -> new User(a.getKey(), a.getValue())).collect(Collectors.toList());
return userList;
}
/**
*根据key倒序排序
*/
public static List<User> reverseOrderByKey(){
List<User> userList = mapToList.entrySet().stream().sorted(Map.Entry.comparingByKey(Comparator.reverseOrder())).
map(a -> new User(a.getKey(),a.getValue())).collect(Collectors.toList());
return userList;
}
/**
*根据key排序,方法2
*/
public static List<User> orderByKeyMethodTwo(){
List<User> userList = mapToList.entrySet().stream().sorted(Map.Entry.comparingByKey()).
map(a -> new User(a.getKey(),a.getValue())).collect(Collectors.toList());
return userList;
}
/**
*根据key排序,方法1
*/
private static List<User> orderByKeyMethodOne() {
List<User> userList = mapToList.entrySet().stream().sorted(Comparator.comparing(a -> a.getKey())).
map(a -> new User(a.getKey(),a.getValue())).collect(Collectors.toList());
return userList;
}
/**
* 按照默认顺序
*/
private static List<User> defaultOrder() {
List<User> userList = mapToList.entrySet().stream().map(a -> new User(a.getKey(), a.getValue())).
collect(Collectors.toList());
return userList;
}
}
测试结果
List转Map代码
List转Map
java
package cn.wideth.collect;
import lombok.extern.slf4j.Slf4j;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;
@Slf4j
public class StreamListToMap {
private static final List<User> userList;
static
{
userList = Arrays.asList(
new User(1003,"keko"),
new User(1001,"jeek"),
new User(1002,"mack")
);
}
public static void main(String[] args) {
Map<Integer, String> listToMap1 = method01();
System.out.println(listToMap1);
Map<Integer, User> listToMap2 = method02();
System.out.println(listToMap2);
Map<Integer, User> listToMap3 = method03();
System.out.println(listToMap3);
Map<Integer, User> listToMap4 = method04();
System.out.println(listToMap4);
}
/**
* 指定key-value,value是对象中的某个属性值
*/
public static Map<Integer,String> method01(){
Map<Integer, String> userMap = userList.stream().collect(Collectors.
toMap(User::getId, User::getName));
return userMap;
}
/**
*指定key-value,value是对象本身,User->User 是一个返回本身的lambda表达式
*/
public static Map<Integer,User> method02(){
Map<Integer, User> userMap = userList.stream().collect(Collectors.
toMap(User::getId, User->User));
return userMap;
}
/**
* 指定key-value,value是对象本身,Function.identity()是简洁写法,也是返回对象本身
*/
public static Map<Integer,User> method03(){
Map<Integer, User> 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<Integer,User> method04(){
Map<Integer, User> userMap = userList.stream().collect(Collectors.
toMap(User::getId, Function.identity(),(key1,key2)->key2));
return userMap;
}
}
测试结果