JDK8StreamAPI

  • 测试数据
   private static final List<User> users;

    static {
        users = new ArrayList<>();
        users.add(new User("1", "zs", "山东", "111", 20, 200));
        users.add(new User("2", "ls", "山东", "222", 30, 700));
        users.add(new User("3", "ww", "河南", "333", 40, 2000));
        users.add(new User("4", "zl", "广州", "444", 50, 2700));
        users.add(new User("5", "lq", "广州", "555", 60, 3500));
        users.add(new User("6", "xb", "山东", "222", 70, 4200));
        users.add(new User("7", "lj", "北京", "666", 80, 1100));
    }
  • StreamApi中间操作 filter distinct skip limit map sorted peed
 @Test
    public void test1() throws Exception {
        //过滤出工资大于1500的人,排序,去重,跳过第一个,取2条
        users.stream().filter(u -> u.getSalary() > 1500)
                .map(User::getSalary)
                .sorted((s1, s2) -> s2 - s1)
                .distinct()
                .skip(1)
                .limit(2)
                .forEach(System.out::println);

    }
  • 终止操作 forEach allMatch noneMatch anyMatch findFirst findAny count max min
 @Test
    public void test2() throws Exception {
        //判断所有人的住址是不是都是山东
        boolean allMatch = users.stream().allMatch(u -> "山东".equals(u.getAddress()));
        System.out.println("allMatch = " + allMatch);
        //判断所有人的工资是不是都小于10000
        boolean noneMatch = users.stream().noneMatch(u -> u.getSalary() > 10000);
        System.out.println("noneMatch = " + noneMatch);
        // 判断是否有一个人的住址是河南
        boolean anyMatch = users.stream().anyMatch(u -> "河南".equals(u.getAddress()));
        System.out.println("anyMatch = " + anyMatch);

        //返回工资最高的人员
        User maxSal = users.stream().sorted((u1, u2) -> u2.getSalary() - u1.getSalary()).findFirst().get();
        System.out.println("maxSal = " + maxSal);

        //随机返回一个元素
        User userAny = users.stream().findAny().get();
        System.out.println("userAny = " + userAny);

        //统计工资超过2000的人个数
        long count = users.stream().filter(u -> u.getSalary() > 2000).count();
        System.out.println("count = " + count);

        //统计最高工资
        int maxSalInt = users.stream().mapToInt(User::getSalary).max().getAsInt();

        //统计最低年龄
        int minAge = users.stream().mapToInt(User::getAge).min().getAsInt();
        System.out.println("minAge = " + minAge);

    }
  • 分组与分区 partitioninng groupby
  @Test
    public void test3() throws Exception {
        //分区:工资3000以上和3000以下的
        Map<Boolean, List<User>> collect = users.stream().collect(Collectors.partitioningBy(u -> u.getSalary() > 3000));
        System.out.println("大于3000的人员:" + collect.get(true));
        System.out.println("小于3000的人员:" + collect.get(false));

        //根据人员的地址分组
        Map<String, List<User>> listMap = users.stream().collect(Collectors.groupingBy(User::getAddress));
        listMap.forEach((key, value) -> {
            System.out.println(key + value);
        });

        //根据人员年龄段分组,并统计各个年龄段人数(0-20](20-60] (60-~)
        Map<String, List<User>> ageMap = users.stream().collect(Collectors.groupingBy(u -> {
            Integer age = u.getAge();
            if ((age > 0) && (age <= 20)) {
                return "0-20";
            } else if ((age > 20) && (age <= 60)) {
                return "20-60";
            } else {
                return "60以上";
            }
        }));
        Map<String, Long> data = new HashMap<>();
        ageMap.forEach((key, value) -> {
            long count = value.stream().count();
            data.put(key, count);
        });

        data.keySet().stream().forEach(k -> {
            System.out.println(k + " : " + data.get(k));
        });

    }

  • 收集操作 set list map
  @Test
    public void test4() throws Exception {
        users.stream().collect(Collectors.toList()).forEach(System.out::println);
        users.stream().collect(Collectors.toSet()).forEach(System.out::println);

        users.stream().collect(Collectors.toMap( (User::getId),( (u) -> u ) )).forEach(
                (k,v) -> {
                    System.out.println(k + v);
                }
        );

    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值