java8 List steam

阅读更多
1、抽取对象中所有的id的集合
List list;
List idList = list.stream().map(User::getId).collect(Collectors.toList());
2、List转map
List list ;
Map map = list.stream().collect(Collectors.toMap(e->e.getUserId(),e->e));
Map key值冲突处理
Map map = list.stream().collect(Collectors.toMap(e->e.getUserId(),e->e,(key1 , key2)-> key2));
3、去重
List list;
list= list.stream().distinct().collect(Collectors.toList());
list转array
String[] ss = listStrings.stream().toArray(String[]::new);
4、map转list
List list = map.entrySet().stream().sorted(Map.Entry.comparingByKey())
.map(e -> new Person(e.getKey(), e.getValue())).collect(Collectors.toList());

你可能感兴趣的:(java8 List steam)