2019-06-20 集合操作lamdba

@Test
    public void lamdbaTest() {
        List list = new ArrayList<>();
        list.add(new Demo(1, 2));
        list.add(new Demo(3, 2));
        list.add(new Demo(6, 1));
        list.add(new Demo(2, 2));
        list.add(new Demo(4, 2));
        list.add(new Demo(5, 1));
        list.add(new Demo(8, 1));
        list.add(new Demo(7, 1));
        list.add(new Demo(9, 3));
        Map map = new HashMap<>();

        Stream peek = list.stream().peek(demo -> map.put(demo.getId(), demo));

        // 分组
        Map> collect = list.stream().collect(
                groupingBy(Demo::getOutId)
        );


        // 分组排序
        Map> collectSort = list.stream().collect(
                groupingBy(Demo::getOutId,
                        collectingAndThen(toList(),
                                l -> {
                                    Collections.sort(l, Comparator.comparingInt(Demo::getId));
                                    return l;
                                }))
        );

        // 分组排序 分会排序字段
        Map> collectSortInt = list.stream().collect(
                groupingBy(Demo::getOutId,
                        mapping(Demo::getId, collectingAndThen(toList(),
                                l -> {
                                    Collections.sort(l);
                                    return l;
                                }))));
        // 创建map
        Map demoMap = list.stream()
                .collect(Collectors.toMap(Demo::getId, demo -> demo));

        // 过滤
        List filter = list.stream().filter(demo -> demo.getId() > 3)
                .collect(Collectors.toList());

        //第一个大于3的
        Demo filterFirst = list.stream().filter(demo -> demo.getId() > 3).findFirst().orElse(null);

        // 求数量
        long count = list.stream().filter(demo -> demo.getId() > 3).count();

        // 分组最大的ID对象
        Map max = list.stream().collect(
                groupingBy(Demo::getOutId,
                        collectingAndThen(maxBy(Comparator.comparing(Demo::getId)),
                                Optional::get)));

        // 分组最大的ID
        Map maxInt = list.stream().collect(
                groupingBy(Demo::getOutId,
                        collectingAndThen(maxBy(Comparator.comparing(Demo::getId)),
                                option -> option.get().getId())));


        // 分组mapMap
        Map> mapMap = list.stream().collect(
                groupingBy(Demo::getOutId,
                        toMap(Demo::getId, demo -> demo)));

        int[] ints = IntStream.builder().add(1).build().toArray();
        Demo maxDemo = Collections.max(list, Comparator.comparing(Demo::getId));

        List sorted = list.stream().sorted(Comparator.comparing(Demo::getId))
                .collect(Collectors.toList());
        List sortedLimit = list.stream().sorted(Comparator.comparing(Demo::getId))
                .limit(3)
                .collect(Collectors.toList());
        // 求和
        int sum = list.stream().mapToInt(Demo::getId).sum();
        // 求和
        int sum1 = list.stream().mapToInt(Demo::getId)
                .reduce(Integer::sum)
                .orElse(0);
        // 求最大值
        int sum2 = list.stream().mapToInt(Demo::getId)
                .reduce(Integer::max)
                .orElse(0);
        // 洗牌
        Collections.shuffle(list);
        // 截取列表
        List subList = list.subList(3, 5);
        System.out.println("12345678");
    }

你可能感兴趣的:(2019-06-20 集合操作lamdba)