Java8:stream流介绍与实例


在这里插入图片描述

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);
		}
	}

案例:求和

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值