Java8 stream()特性使用总结
简介
stream()可以对数组/集合操作,提供排序、去重、分组、过滤等多种操作。
先初始化list,以对象集合为例:
static List<Student> students = new ArrayList<>();
static {
students.add(new Student(1,"学生A", 20, 2, 98.0));
students.add(new Student(2,"学生B", 19, 1, 91.0));
students.add(new Student(3,"学生C", 23, 1, 90.0));
students.add(new Student(4,"学生D", 20, 2, 76.0));
students.add(new Student(5,"学生E", 21, 2, 91.0));
students.add(new Student(6,"学生F", 18, 1, 65.0));
students.add(new Student(7,"学生G", 20, 2, 80.0));
students.add(new Student(8,"学生H", 19, 2, 78.0));
students.add(new Student(9,"学生I", 18, 1, 67.0));
students.add(new Student(10,"学生J", 17, 1, 87.0));
}
1.升序,降序sort使用:
// 按年龄升序排序(自然排序)
List<String> newList = students.stream().sorted(Comparator.comparing(Student::getAge)).map(Student::getName).collect(Collectors.toList());
// 按年龄倒序排序
List<String> newList2 = students.stream().sorted(Comparator.comparing(Student::getAge).reversed()).map(Student::getName).collect(Collectors.toList());
// 先按年龄再按分数升序排序
List<String> newList3 = students.stream().sorted(Comparator.comparing(Student::getAge).thenComparing(Student::getScore)).map(Student::getName).collect(Collectors.toList());
// 先按年龄再按分数自定义排序(降序)
List<String> newList4 = students.stream().sorted((p1, p2) -> {
if (p1.getAge() == p2.getAge()) {
return (int) (p2.getScore() - p1.getScore());
} else {
return p2.getAge() - p1.getAge();
}
}).map(Student::getName).collect(Collectors.toList());
System.out.println("按年龄升序排序:" + newList);
System.out.println("--------------------------");
System.out.println("按年龄倒序排序:" + newList2);
System.out.println("--------------------------");
System.out.println("先按年龄再按分数升序排序:" + newList3);
System.out.println("--------------------------");
System.out.println("先按年龄再按分数自定义排序:" + newList4);
/**
* 按工资升序排序:[学生J, 学生F, 学生I, 学生B, 学生H, 学生A, 学生D, 学生G, 学生E, 学生C]
* --------------------------
* 按工资降序排序:[学生C, 学生E, 学生A, 学生D, 学生G, 学生B, 学生H, 学生F, 学生I, 学生J]
* --------------------------
* 先按工资再按年龄升序排序:[学生J, 学生F, 学生I, 学生H, 学生B, 学生D, 学生G, 学生A, 学生E, 学生C]
* --------------------------
* 先按工资再按年龄自定义降序排序:[学生C, 学生E, 学生A, 学生G, 学生D, 学生B, 学生H, 学生I, 学生F, 学生J]
*/
2.取年龄最大值max,最小值min,规约reduce用法:
Optional<Student> collect1 = students.stream().collect(Collectors.maxBy((s1, s2) -> s1.getAge() - s2.getAge()));
Optional<Student> collect2 = students.stream().collect(Collectors.minBy((s1, s2) -> s1.getAge() - s2.getAge()));
System.out.println("max年龄的学生==>" + collect1.get());
System.out.println("min年龄的学生==>" + collect2.get());
/**
* max年龄的学生==>Student(name=学生C, age=23, sex=1, score=90.0)
* min年龄的学生==>Student(name=学生J, age=17, sex=1, score=87.0)
*/
List<Integer> list = Arrays.asList(1, 2, 3, 4);
//求和
Optional<Integer> reduce = list.stream().reduce((x,y) -> x+ y);
System.out.println("求和:"+reduce);
//求积
Optional<Integer> reduce2 = list.stream().reduce((x,y) -> x * y);
System.out.println("求积:"+reduce2);
//求最大值
Optional<Integer> reduce3 = list.stream().reduce((x,y) -> x>y?x:y);
System.out.println("求最大值:"+reduce3);
/**
* 求和:Optional[10]
* 求积:Optional[24]
* 求最大值:Optional[4]
*/
3.取总分数summary,平均分average用法(两种方法):
//方法一
Double summingScore = students.stream().collect(Collectors.summingDouble(Student::getScore));
Double averagingScore = students.stream().collect(Collectors.averagingDouble(Student::getScore));
System.out.println("学生的总分==>" + summingScore);
System.out.println("学生的平均分==>" + averagingScore);
/**
* 学生的总分==>823.0
* 学生的平均分==>82.3
*/
//方法二
DoubleSummaryStatistics summarizingDouble = students.stream().collect(Collectors.summarizingDouble(Student::getScore));
System.out.println("sum==>"+summarizingDouble.getSum());
System.out.println("count==>"+summarizingDouble.getCount());
System.out.println("average==>"+summarizingDouble.getAverage());
System.out.println("max==>"+summarizingDouble.getMax());
System.out.println("min==>"+summarizingDouble.getMin());
/**
* sum==>823.0
* count==>10
* average==>82.3
* max==>98.0
* min==>65.0
*/
4.连接字符串joining使用:
String studentsName = students.stream().map(student -> student.getName()).collect(Collectors.joining());
System.out.println(studentsName);
String studentsName2 = students.stream().map(student -> student.getName()).collect(Collectors.joining(","));
System.out.println(studentsName2);
/**
* 学生A学生B学生C学生D学生E学生F学生G学生H学生I学生J
* 学生A,学生B,学生C,学生D,学生E,学生F,学生G,学生H,学生I,学生J
*/
6.分组/条件分组/多级分组/分组统计groupingBy使用:
//根据性别字段分组
Map<Integer, List<Student>> sexMap = students.stream().collect(Collectors.groupingBy(daliy -> daliy.getSex()));
System.out.println(sexMap);
/**
* {1=[Student(id=2, name=学生B, age=19, sex=1, score=91.0), Student(id=3, name=学生C, age=23, sex=1, score=90.0),
* Student(id=6, name=学生F, age=18, sex=1, score=65.0), Student(id=9, name=学生I, age=18, sex=1, score=67.0),
* Student(id=10, name=学生J, age=17, sex=1, score=87.0)],
* 2=[Student(id=1, name=学生A, age=20, sex=2, score=98.0), Student(id=4, name=学生D, age=20, sex=2, score=76.0),
* Student(id=5, name=学生E, age=21, sex=2, score=91.0), Student(id=7, name=学生G, age=20, sex=2, score=80.0),
* Student(id=8, name=学生H, age=19, sex=2, score=78.0)]}
*/
//加条件分组
Map<String, List<Student>> collect = students.stream().collect(Collectors.groupingBy(student -> {
if (student.getAge() > 20) {
return "20岁以上的";
}
return "20以下的";
}));
System.out.println(collect);
/**
* {20以下的=[Student(id=1, name=学生A, age=20, sex=2, score=98.0), Student(id=2, name=学生B, age=19, sex=1, score=91.0),
* Student(id=4, name=学生D, age=20, sex=2, score=76.0), Student(id=6, name=学生F, age=18, sex=1, score=65.0),
* Student(id=7, name=学生G, age=20, sex=2, score=80.0), Student(id=8, name=学生H, age=19, sex=2, score=78.0),
* Student(id=9, name=学生I, age=18, sex=1, score=67.0), Student(id=10, name=学生J, age=17, sex=1, score=87.0)],
* 20岁以上的=[Student(id=3, name=学生C, age=23, sex=1, score=90.0), Student(id=5, name=学生E, age=21, sex=2, score=91.0)]}
*/
//统计20岁年龄以上和20以下的学生男/女分别有多少
Map<Integer, Map<String, List<Student>>> collect = students.stream().collect(Collectors.groupingBy(Student::getSex, Collectors.groupingBy(student -> {
if (student.getAge() > 20) {
return "20以上的";
}
return "20岁以下的";
})));
System.out.println(collect);
/**
* {1={20以上的=[Student(id=3, name=学生C, age=23, sex=1, score=90.0)],
* 20岁以下的=[Student(id=2, name=学生B, age=19, sex=1, score=91.0), Student(id=6, name=学生F, age=18, sex=1, score=65.0), Student(id=9, name=学生I, age=18, sex=1, score=67.0), Student(id=10, name=学生J, age=17, sex=1, score=87.0)]},
* 2={20以上的=[Student(id=5, name=学生E, age=21, sex=2, score=91.0)],
* 20岁以下的=[Student(id=1, name=学生A, age=20, sex=2, score=98.0), Student(id=4, name=学生D, age=20, sex=2, score=76.0), Student(id=7, name=学生G, age=20, sex=2, score=80.0), Student(id=8, name=学生H, age=19, sex=2, score=78.0)]}}
*/
//按照性别分组统计人数
Map<Integer, Long> collect = students.stream().collect(Collectors.groupingBy(Student::getSex, Collectors.counting()));
System.out.println(collect);
/**
* {1=5, 2=5}
*/
7.遍历foreach/匹配match/find
// 遍历输出符合条件的元素
students.stream().filter(x -> x.getAge() > 22).forEach(System.out::println);
// 匹配第一个
Optional<Student> findFirst = students.stream().filter(x -> x.getScore() > 90).findFirst();
// 匹配任意(适用于并行流)
Optional<Student> findAny = students.parallelStream().filter(x -> x.getName().contains("A")).findAny();
//Optional<Student> findAny = students.parallelStream().filter(x -> x.getAge()>20).findAny();
// 是否包含符合特定条件的元素
boolean anyMatch = students.stream().anyMatch(x -> x.getAge() < 22);
System.out.println("匹配第一个值:" + findFirst.get());
System.out.println("匹配任意一个值:" + findAny.get());
System.out.println("是否存在大于22的值:" + anyMatch);
/**
* Student(id=3, name=学生C, age=23, sex=1, score=90.0)
* 匹配第一个值:Student(id=1, name=学生A, age=20, sex=2, score=98.0)
* 匹配任意一个值:Student(id=1, name=学生A, age=20, sex=2, score=98.0)
* 是否存在大于22的值:true
*/
8.筛选filter/转list
//筛选年龄大于20的人
students.stream().filter(x -> x.getAge()>20).collect(Collectors.toList()).forEach(System.out::println);
/**
* Student(id=3, name=学生C, age=23, sex=1, score=90.0)
* Student(id=5, name=学生E, age=21, sex=2, score=91.0)
*/
//取学生ID集合
List<Integer> ids = students.stream().map(Student::getId).collect(Collectors.toList());
System.out.println(ids);
/**
* [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
*/
9.map转map使用
//转大小写
List<String> stringList = Arrays.asList("dsdD", "erg", "HGY", "dfg");
List<String> upperCollect = stringList.stream().map(x -> x.toUpperCase(Locale.ROOT)).collect(Collectors.toList());
List<String> lowerCollect = stringList.stream().map(x -> x.toLowerCase(Locale.ROOT)).collect(Collectors.toList());
System.out.println(upperCollect);
System.out.println(lowerCollect);
/**
* [DSDD, ERG, HGY, DFG]
* [dsdd, erg, hgy, dfg]
*/
//集合元素+3
List<Integer> list = Arrays.asList(1, 17, 27, 7);
List<Integer> collect = list.stream().map(x -> x + 3).collect(Collectors.toList());
System.out.println(collect);
/**
* [4, 20, 30, 10]
*/
//分数+5
List<Student> collect1 = students.stream().map(x -> {
x.setScore(x.getScore()+5);
return x;
}).collect(Collectors.toList());
collect1.forEach(p -> System.out.println(p.getScore()));
/**
*103.0
* 96.0
* 95.0...
*/
//按照ID转为map
Map<Integer, Student> checkMap1 = students.stream().collect(Collectors.toMap(Student::getId, Function.identity()));
System.out.println(checkMap1);
//取出年龄>18的用户转为map
Map<String, Student> checkMap2 = students.stream().filter(x -> x.getAge() > 18).collect(Collectors.toMap(Student::getName, y -> y));
System.out.println(checkMap2);
/**
* {1=Student(id=1, name=学生A, age=20, sex=2, score=98.0), 2=Student(id=2, name=学生B, age=19, sex=1, score=91.0)...}
* {学生E=Student(id=5, name=学生E, age=21, sex=2, score=91.0), 学生H=Student(id=8, name=学生H, age=19, sex=2, score=78.0)...}
*/
10.提取/合并concat/跳过skip
String[] arr1 = { "a", "b", "c", "d" };
String[] arr2 = { "d", "e", "f", "g" };
Stream<String> stream1 = Stream.of(arr1);
Stream<String> stream2 = Stream.of(arr2);
// concat:合并两个流 distinct:去重
List<String> newList = Stream.concat(stream1, stream2).distinct().collect(Collectors.toList());
// limit:限制从流中获得前n个数据
List<Integer> collect = Stream.iterate(1, x -> x + 2).limit(10).collect(Collectors.toList());
// skip:跳过前n个数据
List<Integer> collect2 = Stream.iterate(1, x -> x + 2).skip(1).limit(5).collect(Collectors.toList());
System.out.println("流合并:" + newList);
System.out.println("limit:" + collect);
System.out.println("skip:" + collect2);
/**
* 流合并:[a, b, c, d, e, f, g]
* limit:[1, 3, 5, 7, 9, 11, 13, 15, 17, 19]
* skip:[3, 5, 7, 9, 11]
*/
11.取两个集合的差集,交集
//取差集 即 数据集中不包含数据源中的数据
List<String> stringList1 = Arrays.asList("abc", "efd", "tgh", "uji");
List<String> stringList2 = Arrays.asList("sf", "vgf", "abc", "tgh");
List<String> reduce1 = stringList1.stream().filter(item -> !stringList2.contains(item)).collect(Collectors.toList());
System.out.println(reduce1);
//取交集
List<String> reduce2 = stringList1.stream().filter(item -> stringList2.stream().collect(Collectors.toList()).contains(item)).collect(Collectors.toList());
System.out.println(reduce2);
/**
* [efd, uji]
* [abc, tgh]
*/
就总结到这了,方法可以根据需要自取。