文章目录
![在这里插入图片描述](https://img-blog.csdnimg.cn/42e11d2a94c74e2fbd3d7c5189601c58.png)
filter
filter:主要用于流的过滤
map
map:映射,将一个流中的元素按照规则映射到另一个流中
实例1:筛选
筛选成绩及格(>=60)与不合格(<60)的学生姓名
实例中.collect(Collectors.toList())下文会进行讲解
public class Practice1 {
public static void main(String[] args) {
List<Student> studentList = new ArrayList<Student>();
studentList.add(new Student("XiaoMing",19,79.5,"male","101"));
studentList.add(new Student("XiaoFang",20,97.5,"female","101"));
studentList.add(new Student("XiaoYang",19,96.5,"male","101"));
studentList.add(new Student("LiLi",21,59.5,"female","101"));
studentList.add(new Student("XiaoZhang",18,62.0,"male","102"));
studentList.add(new Student("XiaoLiu",19,40.5,"male","102"));
//筛选成绩及格(>=60)的学生姓名
List<String> studentInfo = studentList.stream().filter(x->x.getScore()>=60).map(Student::getName).collect(Collectors.toList());
//筛选成绩不及格(<60)的学生姓名
List<String> studentInfo1 = studentList.stream().filter(x->x.getScore()<60).map(Student::getName).collect(Collectors.toList());
System.out.println("成绩及格学生:"+studentInfo);
System.out.println("成绩不合格学生:"+studentInfo1);
}
}
输出:
collect
collect,顾名思义,收集的意思,stream流中不能存储数据,因此要将流中的数据“收集”到可以存储数据的地方中(如集合,数组等)
Collectors工具类方便我们构建Collector,主要作用就是为流中数据转换成集合类
toList():将流中数据收集到List集合中
toSet():将流中数据收集到Set集合中
toMap():将流中数据收集到Map集合中
toCollection():将流中数据收集到任意集合中
实例2:用list,set,指定集合输出
分别用list和set以及指定集合(ArrayList)输出集合中的偶数
public class Practice1 {
public static void main(String[] args) {
List<Integer> list = Arrays.asList(1,2,4,3,6,5,10,6);
//将结果收集到list中
List<Integer> list1 = list.stream().filter(x->x%2==0).collect(Collectors.toList());
//将结果收集到set中
Set<Integer> set = list.stream().filter(x->x%2==0).collect(Collectors.toSet());
//将结果收集到指定集合中(ArrayList)
ArrayList<Integer> list2 = list.stream().filter(x->x%2==0).collect(Collectors.toCollection(ArrayList::new));
System.out.println("List:"+list1);
System.out.println("ArrayList:"+list2);
System.out.println("Set:"+set);
}
}
实例3:用map输出
在设计的学生集合中,用map输出分数高于70的学生姓名
public class Practice1 {
public static void main(String[] args) {
List<Student> studentList = new ArrayList<Student>();
studentList.add(new Student("XiaoMing",19,79.5,"male","101"));
studentList.add(new Student("XiaoFang",20,97.5,"female","101"));
studentList.add(new Student("XiaoYang",19,96.5,"male","101"));
studentList.add(new Student("LiLi",21,59.5,"female","101"));
studentList.add(new Student("XiaoZhang",18,62.0,"male","102"));
studentList.add(new Student("XiaoLiu",19,40.5,"male","102"));
Map<?,Student> map = studentList.stream().filter(x->x.getScore()>70).collect(Collectors.toMap(Student::getName,x->x));
System.out.println("Map:"+map);
}
}
输出:
数据统计方法
Reduce(规约)
把流缩减成值,进而可以实现数据统计
三种reduce方法
1. Optional<T> reduce(BinaryOperator<T> accumulator);
2. T reduce(T identity, BinaryOperator<T> accumulator);
3. <U> U reduce(U identity, BiFunction<U,? super T,U> accumulator, BinaryOperator<U> combiner);
Collectors统计方法
计数方法:counting
求最大值方法:maxBy
求最小值方法:minBy
求和方法:summingDouble,summingInt,summingLong
求平均值方法:averagingDouble,averagingInt,averagingLong
统计全部数据统计信息方法:summarizingDouble,summarizingInt,summarizingLong
实例:统计个数
求学生集合中分数及格学生的个数
public class Practice1 {
public static void main(String[] args) {
List<Student> studentList = new ArrayList<Student>();
studentList.add(new Student("XiaoMing",19,79.5,"male","101"));
studentList.add(new Student("XiaoFang",20,97.5,"female","101"));
studentList.add(new Student("XiaoYang",19,96.5,"male","101"));
studentList.add(new Student("LiLi",21,59.5,"female","101"));
studentList.add(new Student("XiaoZhang",18,62.0,"male","102"));
studentList.add(new Student("XiaoLiu",19,40.5,"male","102"));
//统计
long count1 = studentList.stream().filter(x->x.getScore()>=60).count();
long count2 = studentList.stream().filter(x->x.getScore()>=60).collect(Collectors.counting());
System.out.println("成绩及格的学生个数为:"+count1+","+count2);
}
}
案例:求最值
求学生集合中年龄最大值
public class Practice1 {
public static void main(String[] args) {
List<Student> studentList = new ArrayList<Student>();
studentList.add(new Student("XiaoMing",19,79.5,"male","101"));
studentList.add(new Student("XiaoFang",20,97.5,"female","101"));
studentList.add(new Student("XiaoYang",19,96.5,"male","101"));
studentList.add(new Student("LiLi",21,59.5,"female","101"));
studentList.add(new Student("XiaoZhang",18,62.0,"male","102"));
studentList.add(new Student("XiaoLiu",19,40.5,"male","102"));
//求学生表中的最高年龄
//用reduce方法求年龄最大值
Integer max1 = studentList.stream().reduce(0,(x,y) -> x>y.getAge()?x:y.getAge(),Integer::max);
Integer max2 = studentList.stream().reduce(0,(x,y) -> x>y.getAge()?x:y.getAge(),(x1,x2)->x1>x2?x1:x2);
Integer max3 = studentList.stream().map(Student::getAge).reduce(Integer::max).get();
//用Collectors的maxBy方法
Optional<Integer> max4 = studentList.stream().map(Student::getAge).collect(Collectors.maxBy(Integer::compare));
System.out.println("学生集合中最大年龄为:"+max1+","+max2+","+max3+","+max4);
}
}