java8 stream的用法总结

1、遍历map

map.forEach((k, v) -> System.out.println("key:value = " + k + ":" + v));

2、map转list

map.entrySet().stream().map(e -> new Person(e.getKey(),e.getValue())).collect(Collectors.toList());

3、list转map

Map<Integer, String> result1 = list.stream().collect(Collectors.toMap(Hosting::getId, Hosting::getName));

stream中的 filter  map  方法都可以对stream进行操作。使用collect方法可以将stream转换为list和map。

注意:用Collectors的toMap方法转换List,一般会遇到两个问题。一个是转换map,key重复问题;另一个是空指针异常,即转为map的value是null。

解决方法:(分三种,具体哪种看业务需求)
(1)重复时用后面的value 覆盖前面的value

Map<Integer, String> result1 = list.stream().collect(Collectors.toMap(Hosting::getId, Hosting::getName,(key1 , key2)-> key2));

(2)重复时将前面的value 和后面的value拼接起来;

Map<Integer, String> result1 = list.stream().collect(Collectors.toMap(Hosting::getId, Hosting::getName,(key1 , key2)-> key1+“,”+key2));

(3)重复时将重复key的数据组成集合

Map<Integer, List<String>> map = list.stream().collect(Collectors.toMap(Hosting::getId,
p -> {
List<String> getNameList = new ArrayList<>();
getNameList.add(p.getName());
return getNameList;
},
(List<String> value1, List<String> value2) -> {
value1.addAll(value2);
return value1;
}
));

System.out.println(map);

4、map将list中的字母转为大写:

List<String> alpha = Arrays.asList("a", "b", "c", "d");
List<String> list = alpha.stream().map(string ->{
String res = string.toUpperCase();
return "stream().map()处理之后:" + res;
}).collect(Collectors.toList());
//list.forEach(System.out::println);
list.stream().forEach(string -> {
System.out.println(System.out::println);
});
运行结果:

stream().map()处理之后:A
stream().map()处理之后:B
stream().map()处理之后:C
stream().map()处理之后:D

5、filter去除list中值为null的

Stream<String> language = Stream.of("java", "python", "node", null, "ruby", null, "php");
List<String> result = language.filter(x -> x!=null).collect(Collectors.toList());
List<String> result = language.filter(Objects::nonNull).collect(Collectors.toList());

6、

//List根据某个字段过滤、排序
listStu.stream()
.filter(student -> student.getSex().equals("女"))
.sorted(Comparator.comparing(Student::getName))
.collect(Collectors.toList());

//List根据某个字段分组
  Map<String,List<Student>> sexGroupMap = listStu.stream()
.collect(Collectors.groupingBy(Student::getSex));
  //如果Map中多个名称相同,则studentId用逗号间隔
  Map<String,String> studentNameIdMap = listStu.stream()
  .collect(toMap(Student::getName,Student::getStuId,(s,a)->s+","+a));
7、遍历循环过滤 以某个字段分组后的map

//声明一个List集合
List<User> list = new ArrayList();
list.add(new User("小A",10,"90"));
list.add(new User( "小B",20,"23"));
list.add(new User("小C",47,"98"));
list.add(new User("小C",34,"90"));
Map<String, List<User>> map = list.stream().collect(Collectors.groupingBy(User::getName));

List<Pesron> AA = new ArrayList<>();
map.forEach((key,value) ->{
value.stream().filter(user -> {
Pesron a = new Pesron();
if (user.getAge() > 10 && "23".equals(user.getScore())) {
a.setScore(user.getScore());
a.setName(key);
AA.add(a);

}
return true;
}
).collect(Collectors.toList());
System.out.println("key--:"+key+"----value:"+value);
});
System.out.println(AA);

 

posted @ 2020-05-31 15:15  pretty.sunshine  Views(1087)  Comments(0Edit  收藏  举报