1. Stream概述
1.1 Stream简介
对于Java8提供的Stream与Lambda表达式结合使用的功能,每个人看法不一,这种语法糖操作确实带来了一定的便利,使代码更加的简洁,但同时也在降低代码的可读性,代码是给人读的不只是机器运行的,所以具体使用看个人的接受程度以及书写风格了。
1.2 Stream分类
Stream 提供的方法非常多,按照调用当前方法是否结束流处理,可以分为中间操作和结束操作。
对于中间操作,又可以分为有状态的操作和无状态操作:
- 无状态的操作是指当前元素的操作不受前面元素的影响。
- 有状态的操作是指当前元素的操作需要等所有元素处理完之后才能进行。
对于结束操作,又可以分为短路操作和非短路操作,具体如下:
- 短路操作是指不需要处理完全部的元素就可以结束。
- 非短路操作是指必须处理完所有元素才能结束。
2. Stream操作
2.1 Stream创建
Stream创建:使用集合创建,使用数组创建,使用 Stream 静态方法
@Test
public void createStream() {
List<Integer> list = Arrays.asList(5, 2, 3, 1, 4);
Stream streamInt = list.stream();
printStream(streamInt, "1.1使用集合创建Stream:");
System.out.println("-----------------------");
String[] array = {"ab", "abc", "abcd", "abcde", "abcdef"};
Stream<String> streamStr = Arrays.stream(array);
printStream(streamStr, "1.2使用数组创建Stream:");
System.out.println("-----------------------");
Stream<String> streamStatic = Stream.of("ab", "abc", "abcd", "abcde", "abcdef");
printStream(streamStatic, "1.3使用 Stream 静态方法Stream:");
System.out.println("-----------------------");
Stream<Integer> stream4 = Stream.iterate(0, (x) -> x + 3).limit(5);
printStream(stream4, "Stream迭代器:");
Stream<Integer> stream5 = Stream.generate(new Random()::nextInt).limit(3);
printStream(stream5, "Stream构建器:");
System.out.println("-----------------------");
}
2.2 Stream无状态操作
Stream无状态操作:map,mapToXXX,flatMap,flatMapToXXX,filter,peek,unordered
@Test
public void operationStreamStateless() {
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("案例1:对整数数组每个元素加3的值:" + newList);
List<String> listStr = Arrays.asList("ab", "abc", "abcd", "abcde", "abcdef");
List<String> newListStr = listStr.stream().map(String::toUpperCase).collect(Collectors.toList());
System.out.println("案例2:把字符串数组的每个元素转换为大写:" + newListStr);
System.out.println("-----------------------");
List<String> listStr3 = Arrays.asList("ab", "abc", "abcd", "abcde", "abcdef");
int[] newList3 = listStr3.stream().mapToInt(r -> r.length()).toArray();
System.out.println("案例3:把字符串数组转为整数数组: " + Arrays.toString(newList3));
System.out.println("-----------------------");
List<String> list4 = Arrays.asList("ab-abc-abcd-abcde-abcdef", "5-2-3-1-4");
List<String> newList4 = list4.stream().flatMap(s -> Arrays.stream(s.split("-"))).collect(Collectors.toList());
System.out.println("案例4:把一个字符串数组转成另一个字符串数组 " + newList4);
System.out.println("-----------------------");
int[][] data = {{1, 2}, {3, 4}, {5, 6}};
IntStream intStream = Arrays.stream(data).flatMapToInt(row -> Arrays.stream(row));
System.out.println("案例5:对给定的二维整数数组求和:" + intStream.sum());
System.out.println("-----------------------");
List<Student> students = buildStudentList();
List<String> nameList = students.stream().filter(x -> x.getScore() >= 90).map(Student::getName).collect(Collectors.toList());
System.out.println("案例6:找出考试成绩在90分以上的学生姓名:" + nameList);
System.out.println("-----------------------");
List<String> collect = Stream.of("one", "two", "three", "four")
.filter(e -> e.length() > 3)
.peek(e -> System.out.print("," + e))
.map(String::toUpperCase)
.peek(e -> System.out.println("->" + e))
.collect(Collectors.toList());
System.out.println("案例7:过滤出stream中长度大于3的字符串并转为大写:" + collect);
System.out.println("-----------------------");
System.out.print("案例8:把一个有序数组转成无序:");
Arrays.asList("1", "2", "3", "4", "5")
.parallelStream()
.unordered()
.forEach(r -> System.out.print(r + " "));
System.out.println("\n-----------------------");
}
2.3 Stream有状态操作
Stream有状态操作:distinct,limit,skip,sorted
@Test
public void operationStreamState() {
String[] array = {"a", "b", "b", "c", "c", "d", "d", "e", "e"};
List<String> newList = Arrays.stream(array).distinct().collect(Collectors.toList());
System.out.println("案例9 :去掉字符串数组中的重复字符串:" + newList);
System.out.println("-----------------------");
String[] array10 = {"c", "c", "a", "b", "b", "e", "e", "d", "d"};
List<String> newList10 = Arrays.stream(array10).limit(5).collect(Collectors.toList());
System.out.println("案例10 :从数组中获取前 5 个元素:" + newList10);
System.out.println("-----------------------");
String[] array11 = {"a", "b", "c", "d", "e", "f", "g", "h", "i"};
List<String> newList11 = Arrays.stream(array11).skip(5).collect(Collectors.toList());
System.out.println("案例11:从数组中获取前5个元素之后的元素:" + newList11);
System.out.println("-----------------------");
String[] array12 = {"c", "c", "a", "b", "b", "e", "e", "d", "d"};
List<String> newList12 = Arrays.stream(array12).sorted().collect(Collectors.toList());
System.out.println("案例12:对给定数组进行排序:" + newList12);
Integer[] array121 = {3, 6, 5, 4, 1, 2, 2, 3, 6};
List<Integer> newList121 = Arrays.stream(array121).sorted().collect(Collectors.toList());
System.out.println("案例12:对给定数组进行排序:" + newList121);
System.out.println("-----------------------");
List<Student> students = buildStudentList();
List<String> nameList = students.stream().sorted(Comparator.comparing(Student::getScore)).map(Student::getName).collect(Collectors.toList());
System.out.println("案例13:按照学生成绩进行排序:" + nameList);
System.out.println("-----------------------");
}
2.4 Stream短路操作
Stream短路操作:findAny,anyMatch,allMatch,noneMatch,findFirst
@Test
public void operationStreamShortCircuit() {
List<Student> students = buildStudentList();
Optional<Student> studentFindAny = students.stream().filter(x -> x.getScore() > 90).findAny();
System.out.println("案例14:找出任意一个考试成绩在90分以上的学生姓名:" + studentFindAny.orElseGet(null).getName());
System.out.println("-----------------------");
boolean result1 = students.stream().anyMatch(x -> x.getScore() > 90);
System.out.println("案例15:是否存在成绩高于 90 分的学生:" + result1);
boolean result2 = students.stream().anyMatch(x -> x.getScore() < 50);
System.out.println("案例15:是否存在成绩低于 50 分的学生:" + result2);
System.out.println("-----------------------");
boolean result16 = students.stream().allMatch(x -> x.getScore() > 90);
System.out.println("案例16:是否所有学生的成绩都高于90分:" + result16);
boolean result26 = students.stream().allMatch(x -> x.getScore() > 50);
System.out.println("案例16:是否所有学生的成绩都高于50分:" + result26);
System.out.println("-----------------------");
boolean result17 = students.stream().noneMatch(x -> x.getScore() > 90);
System.out.println("案例17:是不是没有学生成绩在 90 分以上:" + result17);
boolean result27 = students.stream().noneMatch(x -> x.getScore() < 50);
System.out.println("案例17:是不是没有学生成绩在 50 分以下:" + result27);
System.out.println("-----------------------");
Optional<Student> studentFindFirst = students.stream().filter(x -> x.getScore() > 90).findFirst();
System.out.println("案例18:第一个成绩在 90 分以上的学生姓名:" + studentFindFirst.orElseGet(null).getName());
System.out.println("-----------------------");
}
2.5 Stream非短路操作
Stream非短路操作:forEach,forEachOrdered,toArray,reduce,collect,max、min、count
@Test
public void operationStreamNonShortCircuit() {
List<Integer> array = Arrays.asList(5, 2, 3, 1, 4);
System.out.print("案例19:遍历一个数组并打印:");
array.stream().forEach(System.out::print);
System.out.println("\n-----------------------");
System.out.print("案例20:用并行流遍历一个数组并按照给定数组的顺序输出结果:");
array.parallelStream().forEachOrdered(System.out::print);
System.out.println("\n-----------------------");
Stream<String> stream = Arrays.asList("ab", "abc", "abcd", "abcde", "abcdef").stream();
String[] newArray1 = stream.toArray(str -> new String[5]);
System.out.println("案例21:把给定字符串流转化成数组:" + Arrays.asList(newArray1));
System.out.println("-----------------------");
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("案例22:数组元素之和:" + sum.get());
System.out.println("案例22:数组元素乘积:" + product.get());
System.out.println("案例22:数组元素最大值:" + max.get());
List<Student> students = buildStudentList();
Optional<Integer> maxScore = students.stream().map(r -> r.getScore()).reduce(Integer::max);
Optional<Integer> sumScore = students.stream().map(r -> r.getScore()).reduce(Integer::sum);
Optional<Integer> minScore = students.stream().map(r -> r.getScore()).reduce(Integer::min);
System.out.println("案例23:全班学生最高分:" + maxScore.get());
System.out.println("案例23:全班学生最低分:" + minScore.get());
System.out.println("案例23:全班学生总分:" + sumScore.get());
System.out.println("-----------------------");
List<String> lists = 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("案例24:全班学生姓名列表:" + lists);
System.out.println("案例24:全班学生不同分数列表:" + set);
System.out.println("案例24:全班学生姓名分数集合:" + map);
System.out.println("-----------------------");
long count = list.stream().collect(Collectors.counting());
int summingInt = list.stream().collect(Collectors.summingInt(r -> r));
double averagingDouble = list.stream().collect(Collectors.averagingDouble(r -> r));
Optional<Integer> maxBy = list.stream().collect(Collectors.maxBy(Integer::compare));
Optional<Integer> minBy = list.stream().collect(Collectors.minBy(Integer::compare));
System.out.println("案例25:总数:" + count);
System.out.println("案例25:总和:" + summingInt);
System.out.println("案例25:平均值:" + averagingDouble);
System.out.println("案例25:最大值:" + maxBy.get());
System.out.println("案例25:最小值:" + minBy.get());
System.out.println("-----------------------");
Map<Boolean, List<Student>> partitionByScore = students.stream().collect(Collectors.partitioningBy(x -> x.getScore() > 80));
System.out.println("案例27:将学生按照考试成绩80分以上分区:" + partitionByScore);
partitionByScore.forEach((k, v) -> {
System.out.print(k ? "80分以上:" : "80分以下:");
v.forEach(r -> System.out.print(r.getName() + ","));
System.out.println();
});
System.out.println("-----------------------");
Map<String, Map<Integer, List<Student>>> group = students.stream().collect(Collectors.groupingBy(Student::getSex, Collectors.groupingBy(Student::getScore)));
System.out.println("案例28:将学生按照性别、年龄分组:");
group.forEach((k, v) -> {
System.out.println("male".equals(k) ? "小哥哥:" : "小姐姐:");
v.forEach((k1, v1) -> {
System.out.print(" " + k1 + ":");
v1.forEach(r -> System.out.print(r.getName() + ","));
System.out.println();
});
});
System.out.println("-----------------------");
String studentNames = students.stream().map(r -> r.getName()).collect(Collectors.joining(","));
System.out.println("案例29:所有学生姓名列表:" + studentNames);
System.out.println("-----------------------");
int listSum = list.stream().collect(Collectors.reducing(0, x -> x + 1, (sums, b) -> sums + b));
System.out.println("案例30:数组中每个元素加1后总和:" + listSum);
System.out.println("-----------------------");
System.out.println("案例31:数组元素最大值:" + list.stream().max(Integer::compareTo).get());
System.out.println("案例31:数组元素最小值:" + list.stream().min(Integer::compareTo).get());
System.out.println("案例31:数组中大于3的元素个数:" + list.stream().filter(x -> x > 3).count());
System.out.println("-----------------------");
Optional<Student> optional = students.stream().max(Comparator.comparing(r -> r.getScore()));
System.out.println("案例32:成绩最高的学生姓名:" + optional.get().getName() + ": " + optional.get().getScore());
System.out.println("-----------end------------");
}
3. 测试验证
StreamTest 包含以上案例,是个完整的测试案例。
package com.jerry.unit.stream;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.junit.Test;
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import java.util.stream.Stream;
public class StreamTest {
@Test
public void createStream() {
List<Integer> list = Arrays.asList(5, 2, 3, 1, 4);
Stream streamInt = list.stream();
printStream(streamInt, "1.1使用集合创建Stream:");
System.out.println("-----------------------");
String[] array = {"ab", "abc", "abcd", "abcde", "abcdef"};
Stream<String> streamStr = Arrays.stream(array);
printStream(streamStr, "1.2使用数组创建Stream:");
System.out.println("-----------------------");
Stream<String> streamStatic = Stream.of("ab", "abc", "abcd", "abcde", "abcdef");
printStream(streamStatic, "1.3使用 Stream 静态方法Stream:");
System.out.println("-----------------------");
Stream<Integer> stream4 = Stream.iterate(0, (x) -> x + 3).limit(5);
printStream(stream4, "Stream迭代器:");
Stream<Integer> stream5 = Stream.generate(new Random()::nextInt).limit(3);
printStream(stream5, "Stream构建器:");
System.out.println("-----------------------");
}
@Test
public void operationStreamStateless() {
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("案例1:对整数数组每个元素加3的值:" + newList);
List<String> listStr = Arrays.asList("ab", "abc", "abcd", "abcde", "abcdef");
List<String> newListStr = listStr.stream().map(String::toUpperCase).collect(Collectors.toList());
System.out.println("案例2:把字符串数组的每个元素转换为大写:" + newListStr);
System.out.println("-----------------------");
List<String> listStr3 = Arrays.asList("ab", "abc", "abcd", "abcde", "abcdef");
int[] newList3 = listStr3.stream().mapToInt(r -> r.length()).toArray();
System.out.println("案例3:把字符串数组转为整数数组: " + Arrays.toString(newList3));
System.out.println("-----------------------");
List<String> list4 = Arrays.asList("ab-abc-abcd-abcde-abcdef", "5-2-3-1-4");
List<String> newList4 = list4.stream().flatMap(s -> Arrays.stream(s.split("-"))).collect(Collectors.toList());
System.out.println("案例4:把一个字符串数组转成另一个字符串数组 " + newList4);
System.out.println("-----------------------");
int[][] data = {{1, 2}, {3, 4}, {5, 6}};
IntStream intStream = Arrays.stream(data).flatMapToInt(row -> Arrays.stream(row));
System.out.println("案例5:对给定的二维整数数组求和:" + intStream.sum());
System.out.println("-----------------------");
List<Student> students = buildStudentList();
List<String> nameList = students.stream().filter(x -> x.getScore() >= 90).map(Student::getName).collect(Collectors.toList());
System.out.println("案例6:找出考试成绩在90分以上的学生姓名:" + nameList);
System.out.println("-----------------------");
List<String> collect = Stream.of("one", "two", "three", "four")
.filter(e -> e.length() > 3)
.peek(e -> System.out.print("," + e))
.map(String::toUpperCase)
.peek(e -> System.out.println("->" + e))
.collect(Collectors.toList());
System.out.println("案例7:过滤出stream中长度大于3的字符串并转为大写:" + collect);
System.out.println("-----------------------");
System.out.print("案例8:把一个有序数组转成无序:");
Arrays.asList("1", "2", "3", "4", "5")
.parallelStream()
.unordered()
.forEach(r -> System.out.print(r + " "));
System.out.println("\n-----------------------");
}
@Test
public void operationStreamState() {
String[] array = {"a", "b", "b", "c", "c", "d", "d", "e", "e"};
List<String> newList = Arrays.stream(array).distinct().collect(Collectors.toList());
System.out.println("案例9 :去掉字符串数组中的重复字符串:" + newList);
System.out.println("-----------------------");
String[] array10 = {"c", "c", "a", "b", "b", "e", "e", "d", "d"};
List<String> newList10 = Arrays.stream(array10).limit(5).collect(Collectors.toList());
System.out.println("案例10 :从数组中获取前 5 个元素:" + newList10);
System.out.println("-----------------------");
String[] array11 = {"a", "b", "c", "d", "e", "f", "g", "h", "i"};
List<String> newList11 = Arrays.stream(array11).skip(5).collect(Collectors.toList());
System.out.println("案例11:从数组中获取前5个元素之后的元素:" + newList11);
System.out.println("-----------------------");
String[] array12 = {"c", "c", "a", "b", "b", "e", "e", "d", "d"};
List<String> newList12 = Arrays.stream(array12).sorted().collect(Collectors.toList());
System.out.println("案例12:对给定数组进行排序:" + newList12);
Integer[] array121 = {3, 6, 5, 4, 1, 2, 2, 3, 6};
List<Integer> newList121 = Arrays.stream(array121).sorted().collect(Collectors.toList());
System.out.println("案例12:对给定数组进行排序:" + newList121);
System.out.println("-----------------------");
List<Student> students = buildStudentList();
List<String> nameList = students.stream().sorted(Comparator.comparing(Student::getScore)).map(Student::getName).collect(Collectors.toList());
System.out.println("案例13:按照学生成绩进行排序:" + nameList);
System.out.println("-----------------------");
}
@Test
public void operationStreamShortCircuit() {
List<Student> students = buildStudentList();
Optional<Student> studentFindAny = students.stream().filter(x -> x.getScore() > 90).findAny();
System.out.println("案例14:找出任意一个考试成绩在90分以上的学生姓名:" + studentFindAny.orElseGet(null).getName());
System.out.println("-----------------------");
boolean result1 = students.stream().anyMatch(x -> x.getScore() > 90);
System.out.println("案例15:是否存在成绩高于 90 分的学生:" + result1);
boolean result2 = students.stream().anyMatch(x -> x.getScore() < 50);
System.out.println("案例15:是否存在成绩低于 50 分的学生:" + result2);
System.out.println("-----------------------");
boolean result16 = students.stream().allMatch(x -> x.getScore() > 90);
System.out.println("案例16:是否所有学生的成绩都高于90分:" + result16);
boolean result26 = students.stream().allMatch(x -> x.getScore() > 50);
System.out.println("案例16:是否所有学生的成绩都高于50分:" + result26);
System.out.println("-----------------------");
boolean result17 = students.stream().noneMatch(x -> x.getScore() > 90);
System.out.println("案例17:是不是没有学生成绩在 90 分以上:" + result17);
boolean result27 = students.stream().noneMatch(x -> x.getScore() < 50);
System.out.println("案例17:是不是没有学生成绩在 50 分以下:" + result27);
System.out.println("-----------------------");
Optional<Student> studentFindFirst = students.stream().filter(x -> x.getScore() > 90).findFirst();
System.out.println("案例18:第一个成绩在 90 分以上的学生姓名:" + studentFindFirst.orElseGet(null).getName());
System.out.println("-----------------------");
}
@Test
public void operationStreamNonShortCircuit() {
List<Integer> array = Arrays.asList(5, 2, 3, 1, 4);
System.out.print("案例19:遍历一个数组并打印:");
array.stream().forEach(System.out::print);
System.out.println("\n-----------------------");
System.out.print("案例20:用并行流遍历一个数组并按照给定数组的顺序输出结果:");
array.parallelStream().forEachOrdered(System.out::print);
System.out.println("\n-----------------------");
Stream<String> stream = Arrays.asList("ab", "abc", "abcd", "abcde", "abcdef").stream();
String[] newArray1 = stream.toArray(str -> new String[5]);
System.out.println("案例21:把给定字符串流转化成数组:" + Arrays.asList(newArray1));
System.out.println("-----------------------");
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("案例22:数组元素之和:" + sum.get());
System.out.println("案例22:数组元素乘积:" + product.get());
System.out.println("案例22:数组元素最大值:" + max.get());
List<Student> students = buildStudentList();
Optional<Integer> maxScore = students.stream().map(r -> r.getScore()).reduce(Integer::max);
Optional<Integer> sumScore = students.stream().map(r -> r.getScore()).reduce(Integer::sum);
Optional<Integer> minScore = students.stream().map(r -> r.getScore()).reduce(Integer::min);
System.out.println("案例23:全班学生最高分:" + maxScore.get());
System.out.println("案例23:全班学生最低分:" + minScore.get());
System.out.println("案例23:全班学生总分:" + sumScore.get());
System.out.println("-----------------------");
List<String> lists = 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("案例24:全班学生姓名列表:" + lists);
System.out.println("案例24:全班学生不同分数列表:" + set);
System.out.println("案例24:全班学生姓名分数集合:" + map);
System.out.println("-----------------------");
long count = list.stream().collect(Collectors.counting());
int summingInt = list.stream().collect(Collectors.summingInt(r -> r));
double averagingDouble = list.stream().collect(Collectors.averagingDouble(r -> r));
Optional<Integer> maxBy = list.stream().collect(Collectors.maxBy(Integer::compare));
Optional<Integer> minBy = list.stream().collect(Collectors.minBy(Integer::compare));
System.out.println("案例25:总数:" + count);
System.out.println("案例25:总和:" + summingInt);
System.out.println("案例25:平均值:" + averagingDouble);
System.out.println("案例25:最大值:" + maxBy.get());
System.out.println("案例25:最小值:" + minBy.get());
System.out.println("-----------------------");
Map<Boolean, List<Student>> partitionByScore = students.stream().collect(Collectors.partitioningBy(x -> x.getScore() > 80));
System.out.println("案例27:将学生按照考试成绩80分以上分区:" + partitionByScore);
partitionByScore.forEach((k, v) -> {
System.out.print(k ? "80分以上:" : "80分以下:");
v.forEach(r -> System.out.print(r.getName() + ","));
System.out.println();
});
System.out.println("-----------------------");
Map<String, Map<Integer, List<Student>>> group = students.stream().collect(Collectors.groupingBy(Student::getSex, Collectors.groupingBy(Student::getScore)));
System.out.println("案例28:将学生按照性别、年龄分组:");
group.forEach((k, v) -> {
System.out.println("male".equals(k) ? "小哥哥:" : "小姐姐:");
v.forEach((k1, v1) -> {
System.out.print(" " + k1 + ":");
v1.forEach(r -> System.out.print(r.getName() + ","));
System.out.println();
});
});
System.out.println("-----------------------");
String studentNames = students.stream().map(r -> r.getName()).collect(Collectors.joining(","));
System.out.println("案例29:所有学生姓名列表:" + studentNames);
System.out.println("-----------------------");
int listSum = list.stream().collect(Collectors.reducing(0, x -> x + 1, (sums, b) -> sums + b));
System.out.println("案例30:数组中每个元素加1后总和:" + listSum);
System.out.println("-----------------------");
System.out.println("案例31:数组元素最大值:" + list.stream().max(Integer::compareTo).get());
System.out.println("案例31:数组元素最小值:" + list.stream().min(Integer::compareTo).get());
System.out.println("案例31:数组中大于3的元素个数:" + list.stream().filter(x -> x > 3).count());
System.out.println("-----------------------");
Optional<Student> optional = students.stream().max(Comparator.comparing(r -> r.getScore()));
System.out.println("案例32:成绩最高的学生姓名:" + optional.get().getName() + ": " + optional.get().getScore());
System.out.println("-----------end------------");
}
private List<Student> buildStudentList() {
List<Student> students = new ArrayList<>();
students.add(new Student("Mike", 10, "male", 88));
students.add(new Student("Jerry", 18, "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));
return students;
}
private void printStream(Stream stream, String msg) {
System.out.print(msg);
stream.forEach(i -> System.out.print(i + ","));
System.out.println();
}
}
@Data
@NoArgsConstructor
@AllArgsConstructor
class Student {
private String name;
private Integer age;
private String sex;
private Integer score;
}
|