java8新特性
大约 6 分钟languagejava
stream
概念介绍
- 无状态操作:当前元素的操作不受前面元素的影响
- 有状态操作:当前元素的操作需要等所有元素处理完之后才能进行
- 短路操作:不需要处理完全部元素,中间就会退出遍历
- 非短路操作:必须处理完所有元素才能结束
创建stream
集合创建 stream
List<Integer> list = Arrays.asList(5, 2, 3, 1, 4);
Stream stream = list.stream();
数组创建stream
String[] array={"ab", "abc", "abcd", "abcde", "abcdef" };
Stream<String> stream = Arrays.stream(array);
使用 stream 静态方法
Stream<String> stream = Stream.of("ab", "abc", "abcd", "abcde", "abcdef");
Stream<Integer> stream2 = Stream.iterate(0, (x) -> x + 3).limit(5);
stream2.forEach(r -> System.out.print(r + " "));
System.out.println();
Stream<Integer> stream3 = Stream.generate(new Random()::nextInt).limit(3);
stream3.forEach(r -> System.out.print(r + " "));
转数组
String[] clientIds = clientList.stream().map(item -> item.getContact_code()).toArray(String[]::new);
Integer[] clientIds = clientList.stream().map(item -> new Integer(item.getContact_code())).toArray(Integer[]::new);
中间操作
有状态操作
distinct
去重
String[] array = { "a", "b", "b", "c", "c", "d", "d", "e", "e"};
List<String> newList = Arrays.stream(array).distinct().collect(Collectors.toList());
System.out.println("newList:" + newList);
limit
类似 mysql 中的 limit ,获取指定的前面几个元素
String[] array = { "c", "c", "a", "b", "b", "e", "e", "d", "d"};
List<String> newList = Arrays.stream(array).limit(5).collect(Collectors.toList());
System.out.println("newList:" + newList);
skip
跳过前 N 元素
String[] array = { "a", "b", "c", "d", "e", "f", "g", "h", "i"};
List<String> newList = Arrays.stream(array).skip(5).collect(Collectors.toList());
System.out.println("newList:" + newList);
sorted
排序
String[] array = { "c", "c", "a", "b", "b", "e", "e", "d", "d"};
List<String> newList = Arrays.stream(array).sorted().collect(Collectors.toList());
System.out.println("newList:" + newList);
无状态操作
filter
过滤符合条件的元素构成新的 stream 返回,可通过 collect() 方法转换为 List
flatMap
接受函数作为入参,将每个元素转换为 stream 然后将这些 stream 融合为一个新的 stream
List<String> list = Arrays.asList("ab-abc-abcd-abcde-abcdef", "5-2-3-1-4");
List<String> newList = list.stream().flatMap(s -> Arrays.stream(s.split("-"))).collect(Collectors.toList());
System.out.println("newList:" + newList);
// 输出结果:newList:[ab, abc, abcd, abcde, abcdef, 5, 2, 3, 1, 4]
使用该特性可以将单词拆分为字母
// 数组
String[] arr = {"See","You"};
// 转换流
Stream<String> stream = Arrays.stream(arr);
Stream<String> stringStream = stream.flatMap(str -> {
/*
将每一个单词按照 ""切割
第一次:See---》["s","e","e"]
第二次:You---》 ["Y","o","u"]
*/
String[] split = str.split("");
// 将字符串数组转换成流,
Stream<String> stream1 = Arrays.stream(split);
return stream1;
});
stringStream.forEach(System.out::println);
flatMapToInt
类似 map 操作,前者操作中没有数据类型的转换,而 flatMapToInt 是将过程中的元素转换为 int 类型后继续操作
flatMaptoLong
flatMaptoDouble
map
接受一个函数作为入参,每个元素都使用该方法进行加工并返回,将这些加工后的新元素构成一个新的 stream 返回 - 之后可通过 collect 方法转换为 list
List<Integer> list = Arrays.asList(5, 2, 3, 1, 4);
List<Integer> newList = list.stream().map(x -> x + 3).collect(Collectors.toList());
System.out.println("newList:" + newList);
// 输出结果:newList:[8, 5, 6, 4, 7]
把字符串数组的每个元素转换为大写
List<String> list = Arrays.asList("ab", "abc", "abcd", "abcde", "abcdef");
List<String> newList = list.stream().map(String::toUpperCase).collect(Collectors.toList());
System.out.println("newList:" + newList);
mapToInt
将字符串集合转换为基本类型数组
List<String> list = Arrays.asList("ab", "abc", "abcd", "abcde", "abcdef");
int[] newList = list.stream().mapToInt(r -> r.length()).toArray();
System.out.println("newList:" + Arrays.toString(newList));
mapToDouble
mapToLong
peek
同其他大多数方法一样,传入一个函数作为参数,对新形成的 stream 进行操作,并且不返回数据,即在链式操作中不会影响之前逻辑形成的结果,用法如下:
Stream.of("one", "two", "three", "four")
.filter(e -> e.length() > 3)
.peek(e -> System.out.println("Filtered value: " + e))
.map(String::toUpperCase)
.peek(e -> System.out.println("Mapped value: " + e))
.collect(Collectors.toList());
unordered
把有序 stream 返回无序 stream,如果原本就是无序的则返回原本的 stream
Arrays.asList("1", "2", "3", "4", "5")
.parallelStream()
.unordered()
.forEach(r -> System.out.print(r + " "));
结束操作
短路操作
遍历到符合条件的数据则截止,不继续遍历剩余元素
findFirst
找出第一个符合条件的元素
Optional<Student> studentFindAny = students.stream().filter(x -> x.getScore() > 90).findFirst();
System.out.print("第一个成绩在 90 分以上的学生姓名:" + studentFindAny.orElseGet(null).getName());
findAny
找到任何一个满足条件的元素则退出
List<Student> students = new ArrayList<>();
students.add(new Student("Mike", 10, "male", 88));
students.add(new Student("Jack", 13,"male", 90));
students.add(new Student("Lucy", 15,"female", 100));
students.add(new Student("Jessie", 12,"female", 78));
students.add(new Student("Allon", 16,"female", 92));
students.add(new Student("Alis", 22,"female", 50));
Optional<Student> studentFindAny = students.stream().filter(x -> x.getScore() > 90).findAny();
System.out.print("找出任意一个考试成绩在90分以上的学生姓名:" + studentFindAny.orElseGet(null).getName());
anyMatch
是否存在任意一个满足条件的元素
boolean result1 = students.stream().anyMatch(x -> x.getScore() > 90);
System.out.println("是否存在成绩高于 90 分的学生:" + result1);
boolean result2 = students.stream().anyMatch(x -> x.getScore() < 50);
System.out.print("是否存在成绩低于 50 分的学生:" + result2);
allMatch
是否所有元素都满足条件,如果源数据为空则返回 true
boolean result1 = students.stream().allMatch(x -> x.getScore() > 90);
System.out.println("是否所有学生的成绩都高于90分:" + result1);
boolean result2 = students.stream().allMatch(x -> x.getScore() > 50);
System.out.print("是否所有学生的成绩都高于50分:" + result2);
noneMatch
是否没有元素满足条件,如果集合为空则返回 true
boolean result1 = students.stream().noneMatch(x -> x.getScore() > 90);
System.out.println("是不是没有学生成绩在 90 分以上:" + result1);
boolean result2 = students.stream().noneMatch(x -> x.getScore() < 50);
System.out.print("是不是没有学生成绩在 50 分以下:" + result2);
非短路操作
forEach
遍历集合
List<Integer> array = Arrays.asList(5, 2, 3, 1, 4);
array.stream().forEach(System.out :: println);
forEachOrdered
按照给定集合元素的顺序输出
List<Integer> array = Arrays.asList(5, 2, 3, 1, 4);
array.parallelStream().forEachOrdered(System.out :: println);
toArray
转换为数组
Stream<String> stream = Arrays.asList("ab", "abc", "abcd", "abcde", "abcdef").stream();
String[] newArray1 = stream.toArray(str -> new String[5]);
String[] newArray2 = stream.toArray(String[]::new);
Object[] newArray3 = stream.toArray();
reduce
规约操作,将集合的所有元素合并为一个元素,例如求和,最大、最小等
List<Integer> list = Arrays.asList(5, 2, 3, 1, 4);
Optional<Integer> sum = list.stream().reduce((x, y) -> x + y);
Optional<Integer> product = list.stream().reduce((x, y) -> x * y);
Optional<Integer> max = list.stream().reduce((x, y) -> x > y ? x : y);
System.out.println("数组元素之和:" + sum.get());
System.out.println("数组元素乘积:" + product.get());
System.out.println("数组元素最大值:" + max.get());
collect
归结到新的集合
List<Student> students = new ArrayList<>();
students.add(new Student("Mike", 10, "male", 88));
students.add(new Student("Jack", 13,"male", 90));
students.add(new Student("Lucy", 15,"female", 100));
students.add(new Student("Jessie", 12,"female", 88));
students.add(new Student("Allon", 16,"female", 92));
students.add(new Student("Alis", 22,"female", 50));
List<String> list = students.stream().map(r -> r.getName()).collect(Collectors.toList());
Set<Integer> set = students.stream().map(r -> r.getScore()).collect(Collectors.toSet());
Map<String, Integer> map = students.stream().collect(Collectors.toMap(Student::getName, Student::getScore));
System.out.println("全班学生姓名列表:" + list);
System.out.println("全班学生不同分数列表:" + set);
System.out.println("全班学生姓名分数集合:" + map);
统计
List<Integer> list = Arrays.asList(5, 2, 3, 1, 4);
long count = list.stream().collect(Collectors.counting());
int sum = list.stream().collect(Collectors.summingInt(r -> r));
double average = list.stream().collect(Collectors.averagingDouble(r -> r));
Optional<Integer> max = list.stream().collect(Collectors.maxBy(Integer::compare));
Optional<Integer> min = list.stream().collect(Collectors.maxBy((x, y) -> x > y ? y : x));
System.out.println("总数:" + count);
System.out.println("总和:" + sum);
System.out.println("平均值:" + average);
System.out.println("最大值:" + max.get());
System.out.println("最小值:" + min.get());
partitioningBy
List<Student> students = new ArrayList<>();
students.add(new Student("Mike", 10, "male", 88));
students.add(new Student("Jack", 10,"male", 90));
students.add(new Student("Lucy", 12,"female", 100));
students.add(new Student("Jessie", 12,"female", 78));
students.add(new Student("Allon", 16,"female", 92));
students.add(new Student("Alis", 16,"female", 50));
Map<Boolean, List<Student>> partitionByScore = students.stream().collect(Collectors.partitioningBy(x -> x.getScore() > 80));
System.out.println("将学生按照考试成绩80分以上分区:");
partitionByScore.forEach((k,v ) -> {
System.out.print(k ? "80分以上:" : "80分以下:");
v.forEach(r -> System.out.print(r.getName() + ","));
System.out.println();
});
System.out.println();
groupingBy
Map<String, Map<Integer, List<Student>>> group = students.stream().collect(Collectors.groupingBy(Student::getSex, Collectors.groupingBy(Student::getAge)));
System.out.println("将学生按照性别、年龄分组:");
group.forEach((k,v ) -> {
System.out.println(k +":");
v.forEach((k1,v1) -> {
System.out.print(" " + k1 + ":" );
v1.forEach(r -> System.out.print(r.getName() + ","));
System.out.println();
});
});
joining
String studentNames = students.stream().map(r -> r.getName()).collect(Collectors.joining(","));
System.out.println("所有学生姓名列表:" + studentNames);
规约
List<Integer> list = Arrays.asList(5, 2, 3, 1, 4);
int listSum = list.stream().collect(Collectors.reducing(0, x -> x + 1, (sum, b) -> sum + b));
System.out.println("数组中每个元素加 1 后总和:" + listSum);
max、min、count
List<Integer> list = Arrays.asList(5, 2, 3, 1, 4);
System.out.println("数组元素最大值:"+list.stream().max(Integer::compareTo).get());
System.out.println("数组元素最小值:"+list.stream().min(Integer::compareTo).get());
System.out.println("数组中大于3的元素个数:"+list.stream().filter(x -> x > 3).count());
