JDK1.8----Stream流(过滤、分组、循环、映射、排序、计算等方法的详细使用)

本文围绕Java Stream流展开,介绍了创建集合、过滤、分组、排序、循环、转换成map、分页及对象属性去重等操作。如过滤按条件筛选数据,排序分单字段、多字段及空字段处理等情况,还对比了forEach和peek方法的使用情景。

 创建集合

 ArrayList<Student> arrayList = new ArrayList();
        //参数(姓名、分数、年龄)
        arrayList.add(new Student("张三",99.0f,80));
        arrayList.add(new Student("张四",94.0f,81));
        arrayList.add(new Student("张九",97.0f,88));
        arrayList.add(new Student("张八",92.0f,88));
        arrayList.add(new Student("张七",95.0f,86));
        arrayList.add(new Student("张五",98.0f,null));
        arrayList.add(new Student("张六",93.0f,89));

过滤

①、根据条件进行过滤(实际上是返回true则保存数据,返回false则去除数据)

//当年龄大于85的时候,返回true,则保存这条数据
//写法①
List<Student> collect = arrayList.stream()
.filter(o -> o.getAge() > 85).collect(Collectors.toList());
//写法②
List<Student> collect = arrayList.stream().filter(o ->{
           if ( o.getAge() > 85){
               return true;
           }else return false;
        } ).collect(Collectors.toList());

分组

①、根据名字进行分组

   

Map<String, List<Student>> collect = arrayList.stream()
.collect(Collectors.groupingBy(op ->op.getName()));

②、根据姓名的第二个字开始的数据进行分组

 Map<String, List<Student>> collect = arrayList.stream()
.collect(Collectors.groupingBy(op -> {
   return op.getName().substring(1);
        }));

排序

①、根据字段进行升序排序(不做空字段处理,如果字段为空会报错

List<Student> collect = arrayList.stream()
.sorted(Comparator.comparing(Student::getAge))
.collect(Collectors.toList());

②、根据字段进行降序排序(不做空字段处理,如果字段为空会报错

List<Student> collect = arrayList.stream()
.sorted(Comparator.comparing(Student::getAge).reversed())
.collect(Collectors.toList());

③、根据字段进行降序排序(不做空字段处理,如果字段为空会报错

List<Student> collect = arrayList.stream()
.sorted(Comparator.comparing(Student::getAge).reversed()
.thenComparing(Comparator.comparing(Student::getSco).reversed()))
.collect(Collectors.toList());

④、根据字段进行降序排序(进行空字段处理,空字段放在最末尾)

List<Student> collect = arrayList.stream()
.sorted(Comparator.comparing(Student::getAge,Comparator.nullsLast(Integer::compareTo)).reversed())
.collect(Collectors.toList());

⑤、根据字段进行升序排序(进行空字段处理,空字段放在最开始)

List<Student> collect = arrayList.stream()
.sorted(Comparator.comparing(Student::getAge,Comparator.nullsFirst(Integer::compareTo)))
.collect(Collectors.toList());

⑥、根据字段进行升序排序(进行空字段处理,空字段放在最末尾)

List<Student> collect = arrayList.stream()
.sorted(Comparator.comparing(Student::getAge,Comparator.nullsFirst(Integer::compareTo))
.thenComparing(Comparator.comparing(Student::getSco,Comparator.nullsLast(Float::compareTo)))).collect(Collectors.toList());

循环

①、循环对字段进行赋值并输出,forEach是一个最终操作,在forEach里面的修改会直接修改原始数据

 arrayList.stream().forEach(op->{
            op.setAge(18);
            System.out.println(op);
        });

②、peek方法。该方法主要作用是改变元素的内部状态,但是该方法不是一个最终操作,改变的状态仅仅针对经过流处理的元素,并且最终需要使用一个最终操作来收集数据,原始对象不被影响

arrayList.stream().peek(op->{
            op.setAge(18);
        }).collect(Collectors.groupingBy(Student::getName));

两者的使用情景:如果仅仅是循环改变数据或不对后续做其他操作,可以使用forEach方法;如果不仅仅是进行改变数据,后面还需要进行分组、排序等操作,可以使用peek方法

转换成map

①、转换成map并且对重复值进行处理

key为年龄,value为分数,当年龄相同的时候,key重复,那么就比对分数。分数大的保留

arrayList.stream().collect(Collectors
.toMap(op1->op1.getAge(),op2->op2.getSco(),(sco1,sco2)->sco1>sco2?sco1:sco2));

使用stream流进行分页

①limit(maxSize),产生一个流,其中包含了流中最初的maxSize个元素

②skip(n),产生一个流,其中元素是不包含前n个元素的所有元素

利用这两个可以进行分页,例如从第10条开始后的10个

arrayList.stream().skip(9).limit(10).collect(Collectors.toList());

根据对象某一个属性去重

ArrayList<DocChangeShiftsDoctorNames> distinctData = AllList.stream().collect(Collectors.collectingAndThen(Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(o -> o.getName()))), ArrayList::new));

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值