stream流的实际应用

前言

Java集合Stream 出自 Java8。
可谓是加量不加价,丰富了使用场景,还精简了代码。虽然牺牲了一点可读性,但总体来说是很好用的。以下总结其中的一些用法供大家参考。

public class Student{
   String name;
   String sex;
   String age;
   Integer score;
}

一、List<对象>转List<字符串>

提取年龄集合
List<student> studentList = new ArrayList<>();
List<String> nameList = studentList.stream().map(student::getAge).collect(Collectors.toList());

提取年龄集合并去重
方式一
List<student> studentList = new ArrayList<>();
List<String> nameList = studentList.stream().map(student::getAge).distinct().collect(Collectors.toList());

方式二
Set<String> deptIds = studentList.stream().map(student::getAge).collect(Collectors.toSet());

二、按照某个字段 分组

提示:key为空时,报错
Map<String, List<Student>> map = studentList.stream().collect(Collectors.groupingBy(Student::getName));

三、List<对象>去重

方式一 
List<student> studentList = studentList.stream().filter(student -> student.getAge() != null).collect(Collectors.collectingAndThen(Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(Student::getAge))), ArrayList::new));

方式二 分组转map去重
Map<String, List<Student>> map = studentList.stream().collect(Collectors.groupingBy(Student::getName));

方式三 分组转map去重
Map<String, Student> collect = list.stream().collect(Collectors.toMap(Student::getName, Function.identity(),(key1,key2) -> key2));

四、 List<基础类型>去重

方式一 
Set set = new HashSet<>();
List list = studentList.stream().filter(item -> set.add(item)).collect(Collectors.toList());

方式一延伸:如果需要取出重复数据
List list = studentList.stream().filter(item -> !set.add(item)).collect(Collectors.toList());

方式二
List distinctrs = studentList.stream().distinct().collect(Collectors.toList());

方式三
List distinctrs = studentList.stream().collect(Collectors.toSet());

五、filter匹配过滤

filter() 简单使用

对象匹配过滤
List<User> result1 = userList.stream().filter(v -> v.getName().equals("小明")).collect(Collectors.toList());

简单匹配过滤
List<String> result3 = list.stream().filter(v -> v.contains("A") && v.contains("C")).collect(Collectors.toList());

filter()与findFirst()、 orElse()、forEach()一起使用

User user = list.stream().filter(u -> u.getName().endsWith("sh")).findFirst().orElse(null);

list.stream().filter(u -> u.getName().endsWith("sh")).forEach(u -> System.out.println(u.getName()));

filter()与mapToInt()、sum()一起使用

int sum = list.stream().filter(u -> u.getId() >= 2 && u.getId() <= 4).mapToInt(u -> u.getAge()).sum();

Stream filter()与collect()一起使用

long count = list.stream().filter(u -> u.getName().endsWith("sh")).collect(Collectors.counting());

filter()与reduce()一起使用

list.stream().filter(u -> u.getName().endsWith("sh"))
				.mapToInt(u -> u.getAge()).reduce((a, b) -> a + b)
				.ifPresent(s -> System.out.println("Sum of age of users ending name with 'sh': "+ s));

六、List转Map

List 转成Map<String,String>

 Map<String,String> map =  list.stream().collect(Collectors.toMap(Student::getName,StudentDTO::getAge));

List 转成Map<String,Object>

方式一
Map<String, Student> collect = list.stream().collect(Collectors.toMap(Student::getName, student -> student));

方式二
Map<String, Student> collect = list.stream().collect(Collectors.toMap(Student::getName, Function.identity()));

方式二延伸
Map<String, User> map = list.stream().collect(Collectors.toMap(student -> student.getName() + user.getAge(), Function.identity()));

方式三: key重复时,用第三种方式,后key覆盖前key来解决
Map<String, Student> collect = list.stream().collect(Collectors.toMap(Student::getName, Function.identity(),(key1,key2) -> key2));

List 转成Map<String,List>

方式一
Map<String, List<Student>> map = studentList.stream().collect(Collectors.groupingBy(Student::getName));

方式二
Map<String, List<Student>> collect = list.stream().collect(Collectors.toMap(Student::getName, student -> Collections.singletonList(student)));

七、两个List集合取交集、并集、差集

	List<String> list1 = new ArrayList<String>();
    list1.add("1");
    list1.add("2");
    list1.add("3");
    list1.add("5");
    list1.add("6");

    List<String> list2 = new ArrayList<String>();
    list2.add("2");
    list2.add("3");
    list2.add("7");
    list2.add("8");

    // 交集
    List<String> intersection = list1.stream().filter(item -> list2.contains(item)).collect(toList());
    System.out.println("---交集 intersection---");
    intersection.parallelStream().forEach(System.out :: println);

    // 差集 (list1 - list2)
    List<String> reduce1 = list1.stream().filter(item -> !list2.contains(item)).collect(toList());
    System.out.println("---差集 reduce1 (list1 - list2)---");
    reduce1.parallelStream().forEach(System.out :: println);

    // 差集 (list2 - list1)
    List<String> reduce2 = list2.stream().filter(item -> !list1.contains(item)).collect(toList());
    System.out.println("---差集 reduce2 (list2 - list1)---");
    reduce2.parallelStream().forEach(System.out :: println);

    // 并集
    List<String> listAll = list1.parallelStream().collect(toList());
    List<String> listAll2 = list2.parallelStream().collect(toList());
    listAll.addAll(listAll2);
    System.out.println("---并集 listAll---");
    listAll.parallelStream().forEachOrdered(System.out :: println);

    // 去重并集
    List<String> listAllDistinct = listAll.stream().distinct().collect(toList());
    System.out.println("---得到去重并集 listAllDistinct---");
    listAllDistinct.parallelStream().forEachOrdered(System.out :: println);

    System.out.println("---原来的List1---");
    list1.parallelStream().forEachOrdered(System.out :: println);
    System.out.println("---原来的List2---");
    list2.parallelStream().forEachOrdered(System.out :: println);
  • 6
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值