Java高级-stream流

1.介绍

stream流可以非常方便与精简的形式遍历集合,实现过滤、排序等功能

2.将List转成Set

stream.collect(Collectors.toSet());

public class Test01 {
    public static void main(String[] args) {
        ArrayList<UserEntity> userEntities = new ArrayList<>();
        userEntities.add(new UserEntity("张三",16));
        userEntities.add(new UserEntity("李四", 51));
        userEntities.add(new UserEntity("王五", 73));
        userEntities.add(new UserEntity("赵六", 12));
        userEntities.add(new UserEntity("赵六", 12));

        // 创建stream有两种方式
        // 1.串行流
        // 2.并行流
        // 并行流 比 串行流 效率要高
        Stream<UserEntity> stream = userEntities.stream();
        // 转换成set集合
        Set<UserEntity> userEntitySet = stream.collect(Collectors.toSet());
        userEntitySet.forEach(userEntity -> {
            System.out.println(userEntity);
        });
    }
}

3.将List转成Map

需要指定谁作为key,谁作为value
stream.collect(Collectors.toMap());

public class Test02 {
    public static void main(String[] args) {
        ArrayList<UserEntity> userEntities = new ArrayList<>();
        userEntities.add(new UserEntity("张三",16));
        userEntities.add(new UserEntity("李四", 51));
        userEntities.add(new UserEntity("王五", 73));
        userEntities.add(new UserEntity("赵六", 12));

        // list集合并没有key
        // 指定userName作为key,user对象作为value
        Stream<UserEntity> userEntityStream = userEntities.stream();
        /**
         * new Function<UserEntity(List集合中的类型), String(Map中的Key)>()
         */
        Map<String, UserEntity> collect = userEntityStream.collect(Collectors.toMap(new Function<UserEntity, String>() {
            @Override
            public String apply(UserEntity userEntity) {
                // 设置key的值
                return userEntity.getUserName();
            }
        }, new Function<UserEntity, UserEntity>() {
            @Override
            public UserEntity apply(UserEntity userEntity) {
                // 设置value的值
                return userEntity;
            }
        }));

        collect.forEach(new BiConsumer() {
            @Override
            public void accept(Object key, Object value) {
                System.out.println(key + ": " + value);
            }
        });
    }
}

4.计算求和reduce

public class Test03 {
    public static void main(String[] args) {
        Stream<Integer> integerStream = Stream.of(10, 89, 204, 56);
        Optional<Integer> reduce = integerStream.reduce(new BinaryOperator<Integer>() {
            @Override
            public Integer apply(Integer integer, Integer integer2) {
                return integer + integer2;
            }
        });
        System.out.println(reduce.get());

        System.out.println("================================================");

        // 计算每个user的年龄的总数
        ArrayList<UserEntity> userEntities = new ArrayList<>();
        userEntities.add(new UserEntity("张三",16));
        userEntities.add(new UserEntity("李四", 51));
        userEntities.add(new UserEntity("王五", 73));
        userEntities.add(new UserEntity("赵六", 12));
        userEntities.add(new UserEntity("赵六", 12));
        Stream<UserEntity> userEntityStream = userEntities.stream();
        Optional<UserEntity> sum = userEntityStream.reduce(new BinaryOperator<UserEntity>() {
            @Override
            public UserEntity apply(UserEntity u1, UserEntity u2) {
                return new UserEntity("sum", u1.getAge() + u2.getAge());
            }
        });
        System.out.println(sum.get().getAge());
    }
}

5.查找最大值max和最小值min

public class Test04 {
    public static void main(String[] args) {
        ArrayList<UserEntity> userEntities = new ArrayList<>();
        userEntities.add(new UserEntity("张三",16));
        userEntities.add(new UserEntity("李四", 51));
        userEntities.add(new UserEntity("王五", 73));
        userEntities.add(new UserEntity("赵六", 12));
        userEntities.add(new UserEntity("赵六", 12));

        // 最大值
        Stream<UserEntity> userEntityStream1 = userEntities.stream();
        Optional<UserEntity> max = userEntityStream1.max((o1, o2) -> o1.getAge() - o2.getAge());
        System.out.println("max = " + max.get());

        // 最小值
        Stream<UserEntity> userEntityStream2 = userEntities.stream();
        Optional<UserEntity> min = userEntityStream2.min((o1, o2) -> o1.getAge() - o2.getAge());
        System.out.println("min = " + min.get());
    }
}

6.Match匹配

anyMatch:判断条件里,任意一个元素成功,返回true
AllMatch:判断条件里,所有的都是成功,返回true
noneMatch跟allMatch相反,判断条件里的元素,所有的都不是,返回true

public class Test05 {
    public static void main(String[] args) {
        ArrayList<UserEntity> userEntities = new ArrayList<>();
        userEntities.add(new UserEntity("zhangsan",16));
        userEntities.add(new UserEntity("lisi", 51));
        userEntities.add(new UserEntity("wangwu", 73));
        userEntities.add(new UserEntity("zhaoliu", 12));
        userEntities.add(new UserEntity("zhaoliu", 12));

        Stream<UserEntity> userEntityStream = userEntities.stream();
        boolean b = userEntityStream.noneMatch(new Predicate<UserEntity>() {
            @Override
            public boolean test(UserEntity userEntity) {
                return "zhangsan".equals(userEntity.getUserName());
            }
        });
        System.out.println("b = " + b);
    }
}

7.过滤器 filter

public class Test06 {
    public static void main(String[] args) {
        ArrayList<UserEntity> userEntities = new ArrayList<>();
        userEntities.add(new UserEntity("zhangsan",26));
        userEntities.add(new UserEntity("lisi", 51));
        userEntities.add(new UserEntity("wangwu", 73));
        userEntities.add(new UserEntity("zhaoliu", 12));
        userEntities.add(new UserEntity("zhaoliu", 63));

        Stream<UserEntity> userEntityStream = userEntities.stream();
        userEntityStream.filter(new Predicate<UserEntity>() {
            @Override
            public boolean test(UserEntity userEntity) {
                return "zhaoliu".equals(userEntity.getUserName()) && userEntity.getAge() > 2;
            }
        }).forEach(userEntity -> System.out.println(userEntity));
    }
}

8.分页limit 跳过skip

public class Test07 {
    public static void main(String[] args) {
        ArrayList<UserEntity> userEntities = new ArrayList<>();
        userEntities.add(new UserEntity("zhangsan",26));
        userEntities.add(new UserEntity("lisi", 51));
        userEntities.add(new UserEntity("wangwu", 73));
        userEntities.add(new UserEntity("zhaoliu", 12));
        userEntities.add(new UserEntity("zhaoliu", 63));

        // 只查询前5条
        Stream<UserEntity> userEntityStream1 = userEntities.stream();
        userEntityStream1.limit(3).forEach(userEntity -> System.out.println(userEntity));

        System.out.println("====================");

        // 查询 第2条-4条
        Stream<UserEntity> userEntityStream2 = userEntities.stream();
        userEntityStream2.skip(2).limit(2).forEach(userEntity -> System.out.println(userEntity));
    }
}

9.数据排序 sorted

public class Test08 {
    public static void main(String[] args) {
        ArrayList<UserEntity> userEntities = new ArrayList<>();
        userEntities.add(new UserEntity("zhangsan",26));
        userEntities.add(new UserEntity("lisi", 51));
        userEntities.add(new UserEntity("wangwu", 73));
        userEntities.add(new UserEntity("zhaoliu", 12));
        userEntities.add(new UserEntity("zhaoliu", 63));

        Stream<UserEntity> userEntityStream = userEntities.stream();
        userEntityStream.sorted(new Comparator<UserEntity>() {
            @Override
            public int compare(UserEntity o1, UserEntity o2) {
                return o2.getAge() - o1.getAge();
            }
        }).forEach(userEntity -> System.out.println(userEntity));
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值