jdk 新特性 stream流

jdk 新特性 stream流
复制就能用

import java.util.*;
import java.util.stream.Collectors;

public class Test {
    public static void main(String[] args) {
        //1.使用流中的 disctinct去重
//        List<String> strings = Arrays.asList("abd","bc","efg","abcd","efg","jkl");
//        List<String> collect = strings.stream().distinct().collect(Collectors.toList());
//        System.out.println(collect);
//        2.使用limit获取 skip去除 前n个元素
        List<String> strings = Arrays.asList("abd","bc","efg","abcd","efg","jkl");
//        List<String> collect = strings.stream().limit(4).collect(Collectors.toList());
//        List<String> collect2 = strings.stream().skip(4).collect(Collectors.toList());
//        System.out.println(collect2);
//          3.使用sorted对流中元素做排序处理 按中文拼音
//        List<String> strings = Arrays.asList("abd","bc","efg","abcd","efg","jkl");
//        List<String> collect = strings.stream().sorted(Collections.reverseOrder(Collator.getInstance(Locale.CHINA))).collect(Collectors.toList());
//        System.out.println(collect);
//        4.使用allMath对集合中的数据做处理 如判断不等于空
//        List<String> strings = Arrays.asList("abd","bc","efg","abcd","efg","jkl");
//        boolean b = strings.stream().allMatch(s -> s != "");
//        System.out.println(b);
        5.使用forEach遍历流 比 for 慢1倍
//        List<String> strings = Arrays.asList("abd","bc","efg","abcd","efg","jkl");
//        strings.stream().forEach(s -> System.out.println(s));
//        6.使用tomap做转换 设计主键重复的问题
//        List<String> strings = Arrays.asList("abd","bc","efg","abcd","efg","jkl");
//    strings.stream().collect(Collectors.toMap());
//        11111111111111111111111111111111111111111111111111111111111111111111111
//
        List<Persion> persionList = new ArrayList<>();
        persionList.add(new Persion("p1",8000,23,"meal","北京"));
        persionList.add(new Persion("p2",9000,22,"meal","北京"));
        persionList.add(new Persion("p3",7000,23,"femeal","上海"));
        persionList.add(new Persion("p4",8000,25,"femeal","青岛"));
        persionList.add(new Persion("p5",10000,10,"femeal","内蒙古"));
//        筛选工资大于8000的
      List<String> collet =   persionList.stream().filter(x -> x.salary>8000).map(Persion :: getName).collect(Collectors.toList());
        System.out.println(collet);
//        综合实力2 赛选员工工资最高的
        Optional<Persion> max = persionList.stream().max(Comparator.comparingInt(Persion::getSalary));
        System.out.println(max.get().getName());
//        综合实力3 将员工工资增加1000
        List<Persion> collect = persionList.stream().map(persion -> {
            Persion pn = new Persion();
            pn.setAge(persion.getAge());
            pn.setName(persion.getName());
            pn.setSalary(persion.getSalary() + 1000);
            return pn;
        }).collect(Collectors.toList());
//        综合实力4 求工资和
        Optional<Integer> reduce = persionList.stream().map(Persion::getSalary).reduce(Integer::sum);
//        综合实力5 工资分组
        Map<Integer, List<Persion>> collect1 = persionList.stream().collect(Collectors.groupingBy(x -> x.getSalary()));
        System.out.println(collect);
//        排序
        List<Persion> collect2 = persionList.stream().sorted(Comparator.comparing(Persion::getSalary)).collect(Collectors.toList());
        System.out.println(collect2);


    }
    static class Persion{
        private  String name;
        private  int salary;
        private  int age;
        private  String sex;
        private  String area;

        public Persion() {
        }

        public Persion(String name, int salary, int age, String sex, String area) {
            this.name = name;
            this.salary = salary;
            this.age = age;
            this.sex = sex;
            this.area = area;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public int getSalary() {
            return salary;
        }

        public void setSalary(int salary) {
            this.salary = salary;
        }

        public int getAge() {
            return age;
        }

        public void setAge(int age) {
            this.age = age;
        }

        public String getSex() {
            return sex;
        }

        public void setSex(String sex) {
            this.sex = sex;
        }

        public String getArea() {
            return area;
        }

        public void setArea(String area) {
            this.area = area;
        }
    }
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值