文章目录
前言
流的使用包括三件事:
数据源,集合
中间操作,流水线
终端操作,执行流水线,生成结果
Stream 不是集合元素,它不是数据结构并不保存数据,它是有关算法和计算的,它更像一个高级版本的 Iterator。
获取一个数据源(source)→ 数据转换 → 执行操作获取想要的结果
1. list T为一个对象,取出某一个字段的集合 (map)
DxUser dxUserBuilder = DxUser.builder().id(1L).username("1哥").password("123456").build();
DxUser dxUserBuilder1 = DxUser.builder().id(2L).username("2哥").password("123456").build();
DxUser dxUserBuilder2 = DxUser.builder().id(3L).password("123456").build();
DxUser dxUserBuilder3 = DxUser.builder().id(4L).username("4哥").password("123456").build();
List<DxUser> list = new ArrayList<>();
list.add(dxUserBuilder);
list.add(dxUserBuilder1);
list.add(dxUserBuilder2);
list.add(dxUserBuilder3);
//list取出id列
List<Long> collect = list.stream().map(DxUser::getId).collect(Collectors.toList());
collect.forEach(System.out::println);
//list取出username列
List<String> collect1 = list.stream().map(DxUser::getUsername).collect(Collectors.toList());
collect1.forEach(System.out::println);
//1
//2
//3
//4
//1哥
//2哥
//null
//4哥
2. list T为一个对象,取出某一个字段,判断该字段值,进行分类 (filter)
DxUser dxUserBuilder = DxUser.builder().id(1L).username("1哥").password("123456").build();
DxUser dxUserBuilder1 = DxUser.builder().id(2L).username("2哥").password("123456").build();
DxUser dxUserBuilder2 = DxUser.builder().id(3L).password("123456").build();
DxUser dxUserBuilder3 = DxUser.builder().id(4L).username("4哥").password("123456").build();
List<DxUser> list = new ArrayList<>();
list.add(dxUserBuilder);
list.add(dxUserBuilder1);
list.add(dxUserBuilder2);
list.add(dxUserBuilder3);
//找出id大于2L的
List<DxUser> collect = list.stream().filter(x -> x.getId() > 2).collect(Collectors.toList());
collect.forEach(System.out::println);
//大于2L 和 小于等于2进行 分组
Map<Boolean, List<DxUser>> collect1 = list.stream().collect(Collectors.groupingBy(x -> x.getId() > 2));
System.out.println("===大于2L==");
collect1.get(true).forEach(System.out::println);
System.out.println("===小于等于2L==");
collect1.get(false).forEach(System.out::println);
3. 根据 list 获取 list t对象 (map)
DxUser dxUserBuilder = DxUser.builder().id(1L).username("1哥").password("123456").build();
DxUser dxUserBuilder1 = DxUser.builder().id(2L).username("2哥").password("123456").build();
DxUser dxUserBuilder2 = DxUser.builder().id(3L).password("123456").build();
DxUser dxUserBuilder3 = DxUser.builder().id(4L).username("4哥").password("123456").build();
List<DxUser> list = new ArrayList<>();
list.add(dxUserBuilder);
list.add(dxUserBuilder1);
list.add(dxUserBuilder2);
list.add(dxUserBuilder3);
List<Long> ids = Arrays.asList(2L, 3L, 4L);
List<DxUser> dxUsers = ids.stream().map((x) -> {
// for (DxUser dxUser : list) {
// if(dxUser.getId().equals(x)){
// return dxUser;
// }
// }
// return null;
List<DxUser> dxUserList = list.stream().filter(y -> y.getId().equals(x)).collect(Collectors.toList());
return dxUserList.get(0);
} ).collect(Collectors.toList());
dxUsers.forEach(System.out::println);
4. 根据list T对象某个字段进行归类
- 4.1 有几类就分几类 (Collectors.groupingBy)
- 4.2 分为两类,true为符合条件,false不符合条件 (Collectors.partitioningBy)
DxUser dxUserBuilder = DxUser.builder().id(1L).username("1哥").password("123456").department("java开发").build();
DxUser dxUserBuilder1 = DxUser.builder().id(2L).username("2哥").password("123456").department("c#开发").build();
DxUser dxUserBuilder2 = DxUser.builder().id(3L).password("123456").department("c++开发").build();
DxUser dxUserBuilder3 = DxUser.builder().id(4L).username("4哥").password("123456").department("java开发").build();
List<DxUser> list = new ArrayList<>();
list.add(dxUserBuilder);
list.add(dxUserBuilder1);
list.add(dxUserBuilder2);
list.add(dxUserBuilder3);
//用部门进行分类,以上看出,分出三类
Map<String, List<DxUser>> collect = list.stream().collect(Collectors.groupingBy(DxUser::getDepartment));
collect.forEach((x,y)->{
System.out.println("x: " + x);
System.out.println("y: size:"+ y.size() + y);
});
//x: c++开发
//y: size:1[DxUser(id=3, username=null, password=123456,department=c++开发)]
//x: c#开发
//y: size:1[DxUser(id=2, username=2哥, password=123456,department=c#开发)]
//x: java开发
//y: size:2[DxUser(id=1, username=1哥, password=123456,department=java开发), DxUser(id=4, username=4哥, password=123456,department=java开发)]
System.out.println("==========");
Map<Boolean, List<DxUser>> collect1 = list.stream().collect(Collectors.partitioningBy(x -> x.getDepartment().equals("java开发")));
collect1.forEach((x,y)->{
System.out.println("x: " + x);
System.out.println("y: size:"+ y.size() + y);
});
//x: false
//y: size:2[DxUser(id=2, username=2哥, password=123456 department=c#开发), DxUser(id=3, username=null, password=123456 department=c++开发)]
//x: true
//y: size:2[DxUser(id=1, username=1哥, password=123456 department=java开发), DxUser(id=4, username=4哥, password=123456 department=java开发)]
5 对 list 在的T进行替换,想对哪个字段替换就对哪个字段替换(peek)
DxUser dxUserBuilder = DxUser.builder().id(1L).username("1哥").password("123456").department("java开发").build();
DxUser dxUserBuilder1 = DxUser.builder().id(2L).username("2哥").password("123456").department("c#开发").build();
DxUser dxUserBuilder2 = DxUser.builder().id(3L).password("123456").department("c++开发").build();
DxUser dxUserBuilder3 = DxUser.builder().id(4L).username("4哥").password("123456").department("java开发").build();
List<DxUser> list = new ArrayList<>();
list.add(dxUserBuilder);
list.add(dxUserBuilder1);
list.add(dxUserBuilder2);
list.add(dxUserBuilder3);
//把字段进行替换。username+(?),loginname="登录名"
List<Object> collect = list.stream().peek((x) -> {x.setUsername(x.getUsername() + "(?)");x.setLoginname("登录名");}).collect(Collectors.toList());
collect.forEach(System.out::println);
6 排序,对list某个字段进行排序,但是只对基本数据类型有效
- 6.1 正序 (sorted)
- 6.2 倒序
- 6.3 排序后再分组 (sorted,groupingBy)
DxUser dxUserBuilder = DxUser.builder().id(1L).username("1哥").password("3").department("java开发").build();
DxUser dxUserBuilder1 = DxUser.builder().id(-2L).username("2哥").password("4").department("c#开发").build();
DxUser dxUserBuilder2 = DxUser.builder().id(13L).password("13").department("c++开发").build();
DxUser dxUserBuilder3 = DxUser.builder().id(4L).username("4哥").password("-1").department("java开发").build();
List<DxUser> list = new ArrayList<>();
list.add(dxUserBuilder);
list.add(dxUserBuilder1);
list.add(dxUserBuilder2);
list.add(dxUserBuilder3);
//按照id进行排序
List<DxUser> collect = list.stream().sorted(Comparator.comparing(DxUser::getId)).collect(Collectors.toList());
collect.forEach(System.out::println);
System.out.println("-------------------");
//倒序
List<DxUser> collect1 = list.stream().sorted(Comparator.comparing(DxUser::getId).reversed()).collect(Collectors.toList());
collect1.forEach(System.out::println);
System.out.println("-------------------");
//分组后再排序
Map<String, List<DxUser>> collect2 = list.stream().sorted(Comparator.comparing(DxUser::getId).reversed()).collect(Collectors.groupingBy(DxUser::getDepartment));
collect2.forEach((x,y)->{
System.out.println("x: " + x);
System.out.println("y: size:"+ y.size() + y);
});
7 遍历List后,向某个字段进行赋值
DxUser dxUserBuilder = DxUser.builder().id(1L).status(32).username("1哥").password("3").department("java开发").build();
DxUser dxUserBuilder1 = DxUser.builder().id(-2L).status(12).username("2哥").password("4").department("c#开发").build();
DxUser dxUserBuilder2 = DxUser.builder().id(13L).status(10).password("13").department("c++开发").build();
DxUser dxUserBuilder3 = DxUser.builder().id(4L).status(33).username("4哥").password("-1").department("java开发").build();
List<DxUser> list = new ArrayList<>();
list.add(dxUserBuilder);
list.add(dxUserBuilder1);
list.add(dxUserBuilder2);
list.add(dxUserBuilder3);
DxRole dxRole = DxRole.builder().sn("加入成功").build();
List<DxUser> collect = list.stream().peek(x -> {
if ("13".equals(x.getPassword())) {
x.setRole(dxRole);
}
}).collect(Collectors.toList());
collect.forEach(System.out::println);
参考文章
https://www.cnblogs.com/owenma/p/12207330.html
Student s1 = new Student("aa", 10,1);
Student s2 = new Student("bb", 20,2);
Student s3 = new Student("cc", 10,3);
List<Student> list = Arrays.asList(s1, s2, s3);
//装成list
List<Integer> ageList = list.stream().map(Student::getAge).collect(Collectors.toList()); // [10, 20, 10]
//转成set
Set<Integer> ageSet = list.stream().map(Student::getAge).collect(Collectors.toSet()); // [20, 10]
//转成map,注:key不能相同,否则报错
Map<String, Integer> studentMap = list.stream().collect(Collectors.toMap(Student::getName, Student::getAge)); // {cc=10, bb=20, aa=10}
//字符串分隔符连接
String joinName = list.stream().map(Student::getName).collect(Collectors.joining(",", "(", ")")); // (aa,bb,cc)
//聚合操作
//1.学生总数
Long count = list.stream().collect(Collectors.counting()); // 3
//2.最大年龄 (最小的minBy同理)
Integer maxAge = list.stream().map(Student::getAge).collect(Collectors.maxBy(Integer::compare)).get(); // 20
//3.所有人的年龄
Integer sumAge = list.stream().collect(Collectors.summingInt(Student::getAge)); // 40
//4.平均年龄
Double averageAge = list.stream().collect(Collectors.averagingDouble(Student::getAge)); // 13.333333333333334
// 带上以上所有方法
DoubleSummaryStatistics statistics = list.stream().collect(Collectors.summarizingDouble(Student::getAge));
System.out.println("count:" + statistics.getCount() + ",max:" + statistics.getMax() + ",sum:" + statistics.getSum() + ",average:" + statistics.getAverage());
//分组
Map<Integer, List<Student>> ageMap = list.stream().collect(Collectors.groupingBy(Student::getAge));
//多重分组,先根据类型分再根据年龄分
Map<Integer, Map<Integer, List<Student>>> typeAgeMap = list.stream().collect(Collectors.groupingBy(Student::getType, Collectors.groupingBy(Student::getAge)));
//分区
//分成两部分,一部分大于10岁,一部分小于等于10岁
Map<Boolean, List<Student>> partMap = list.stream().collect(Collectors.partitioningBy(v -> v.getAge() > 10));
//规约
Integer allAge = list.stream().map(Student::getAge).collect(Collectors.reducing(Integer::sum)).get(); //40