1、基础介绍
1.1、查找与匹配
allMatch–检查是否全部匹配
anyMatch–检查是否至少匹配一个元素
noneMatch–检查是否没有匹配所有元素
findFirst–返回第一个元素
findAny–返回当前流中的任意元素
count–返回流中元素的总个数
max–返回流中最大数
min–返回流中最小值
List<Student> list = Arrays.asList(
new Student(1,"张三",60,Status.a),
new Student(2,"李四",70,Status.b),
new Student(3,"王五",80,Status.c),
new Student(4,"赵六",90,Status.a),
new Student(5,"田七",100,Status.a));
@Test
public void Test2(){
Long count = list.stream().count();
System.out.println(count);// 输出:5
Optional<Student> optional =
list.stream().max((x,y) -> x.getId().compareTo(y.getId()));
System.out.println(optional.get());// 输出:Student [id=5, name=田七, score=100]
Optional<Integer> optional2 =
list.stream().map(Student::getScore).min(Integer::compareTo);
System.out.println(optional2.get());// 输出:60
Optional<Student> optional3 =
list.stream().min((x,y)->Integer.compare(x.getScore(), y.getScore()));
System.out.println(optional3.get());// 输出:Student [id=1, name=张三, score=60]
}
@Test
public void test1(){
boolean b = list.stream().allMatch((s) -> s.getName().equals(Status.a));
System.out.println(b);// 输出:false
b = list.stream().anyMatch((s) -> s.getName().equals(Status.a));
System.out.println(b);// 输出:false
b = list.stream().noneMatch((s) -> s.getName().equals(Status.a));
System.out.println(b);// 输出:true
Optional<Student> optional =
list.stream().sorted((x,y) -> -Double.compare(x.getId(), y.getId())).findFirst();
System.out.println(optional.get());// 输出:Student [id=5, name=田七, score=100]
optional =
list.parallelStream().filter((e) -> e.getStatus().equals(Status.a)).findAny();
System.out.println(optional.get());// 输出:Student [id=1, name=张三, score=60]
}
1.2、归纳
reduce(T identity, BinaryOperator accumulator) – 可以将流中元素反复结合起来,得到一个值
@Test
public void test3(){
List<Integer> tmp = Arrays.asList(1,2,3,4,5);
Integer sum = tmp.stream().reduce(0, (x,y) -> x + y);
System.out.println(sum);// 输出:15
Optional<Integer> optional = list.stream().map(Student::getScore).reduce(Integer::sum);
System.out.println(optional.get());// 输出:400
}
1.3、收集
collect–将流中转换为其他形式,接收一个Collector接口的实现,用于给Stream中元素做汇总的方法
@Test
public void test4(){
// 转成List
List<String> tmp = list.stream().map(Student::getName)
.collect(Collectors.toList());
tmp.forEach(System.out::print);// 输出:张三李四王五赵六田七
// 转成Set
Set<String> set = list.stream().map(Student::getName)
.collect(Collectors.toSet());
set.forEach(System.out::print);// 输出:张三李四王五赵六田七
// 转成HashSet
HashSet<String> hs = list.stream().map(Student::getName)
.collect(Collectors.toCollection(HashSet::new));
hs.forEach(System.out::print);// 输出:张三李四王五赵六田七
// 获取总数
long count = list.stream().collect(Collectors.counting());
System.out.println(count);// 输出:5
// 获取平均值
double avg = list.stream().collect(Collectors.averagingDouble(Student::getScore));
System.out.println(avg);// 输出:80.0
// 获取总和
double sum = list.stream().collect(Collectors.summingDouble(Student::getId));
System.out.println(sum);// 输出:15.0
// 最大值
Optional<Student> max = list.stream()
.collect(Collectors.maxBy((x,y) -> Integer.compare(x.getId(), y.getId())));
System.out.println(max.get());// 输出:Student [id=5, name=田七, score=100]
// 最小值
Optional<Integer> min = list.stream().map(Student::getScore)
.collect(Collectors.minBy((x,y) -> Integer.compare(x, y)));
System.out.println(min.get());// 输出:60
}
@Test
public void test5(){
// 分组(按状态分)
Map<Status, List<Student>> map =
list.stream().collect(Collectors.groupingBy(Student::getStatus));
System.out.println(map);
// 输出:{b=[Student [id=2, name=李四, score=70]],
// a=[Student [id=1, name=张三, score=60], Student [id=4, name=赵六, score=90], Student [id=5, name=田七, score=100]],
// c=[Student [id=3, name=王五, score=80]]}
// 多级分组(先按状态分,然后在按分数分)
Map<Status, Map<Object, List<Student>>> map1 = list.stream().collect(Collectors.groupingBy(Student::getStatus, Collectors.groupingBy((x) -> {
if(((Student) x).getScore() < 70){
return "及格";
}else if(x.getScore() >= 70 && x.getScore() <= 80){
return "良好";
}else{
return "优秀";
}
})));
System.out.println(map1);
// 输出:{b={良好=[Student [id=2, name=李四, score=70]]},
// a={优秀=[Student [id=4, name=赵六, score=90], Student [id=5, name=田七, score=100]],
// 及格=[Student [id=1, name=张三, score=60]]},
// c={良好=[Student [id=3, name=王五, score=80]]}}
// 分区(按id分)
Map<Boolean, List<Student>> map2 = list.stream().collect(Collectors.partitioningBy((s) -> s.getId() < 4));
System.out.println(map2);
// 输出:{false=[Student [id=4, name=赵六, score=90], Student [id=5, name=田七, score=100]],
// true=[Student [id=1, name=张三, score=60], Student [id=2, name=李四, score=70], Student [id=3, name=王五, score=80]]}
}