2019-04-25——Java8 Stream常用函数

@Data
@AllArgsConstructor
static class Achievement{
    private String name;
    private double score;
    private String date;
}
static List getData(){
    List list = new ArrayList<>();
    list.add(new Achievement("wang",80,"2017-10-20"));
    list.add(new Achievement("wang",90,"2017-5-20"));
    list.add(new Achievement("li",90,"2018-10-20"));
    list.add(new Achievement("zhao",50,"2017-10-20"));
    list.add(new Achievement("qian",60,"2017-10-22"));
    list.add(new Achievement("sun",20,"2017-10-23"));
    list.add(new Achievement("sun",20,"2017-11-23"));
    return list;
}
    /**找出某个人的成绩*/
    static void a1(){
        Achievement achievement = getData().stream().filter(a-> a.getName().equals("wang")).findFirst().get();
        System.out.println(achievement.toString());
    }

    /**是否含有某条数据*/
    static void a2(){
        boolean match = getData().stream().anyMatch(a -> a.getName().equals("liu"));
        System.out.println(match);
    }

    /**按名字分组*/
    static void a3(){
        Map> collect = getData().stream().collect(Collectors.groupingBy(a -> a.getName(), Collectors.toList()));
        System.out.println(collect);
    }

    /**按属性去重*/
    static void a4(){
        ArrayList achievements = getData().stream().collect(Collectors.collectingAndThen(Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(Achievement::getName))), ArrayList::new));
        System.out.println(achievements);
    }

    /**List转Map*/
    static void a5(){
        Map mappings = getData().stream().collect(Collectors.collectingAndThen(Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(Achievement::getName))), ArrayList::new)).stream().collect(Collectors.toMap(a -> a.getName(), a -> a));
        System.out.println(mappings);
    }

    /**List转Map*/
    static void a6(){
        /*策略1:忽略后面的值*/
        Map map = getData().stream().collect(Collectors.toMap(a -> a.getName(), a -> a, (x, y) -> x));
        System.out.println(map);
        /*策略2:使用集合接受重复的属性*/
        Map> collect = getData().stream().collect(Collectors.toMap(a -> a.getName(), (Achievement a) -> {
            List list = new ArrayList<>();
            list.add(a);
            return list;
        }, (x, y) -> {
            x.addAll(y);
            return x;
        }));
        System.out.println(collect);
    }

    /**按多属性升序排序*/
    static void a7(){
        List collect = getData().stream().sorted(Comparator.comparing(Achievement::getName).thenComparing(Achievement::getScore).thenComparing(Achievement::getDate)).collect(Collectors.toList());
        System.out.println(collect);
    }

    /**求分数和*/
    static void a8(){
        double score = getData().stream().mapToDouble(Achievement::getScore).reduce(0d, (x, y) -> x + y);
        System.out.println(score);
    }

    /**分组求和*/
    static void a9(){
        Map collect = getData().stream().collect(Collectors.groupingBy(Achievement::getName, Collectors.summingDouble(Achievement::getScore)));
        System.out.println(collect);
    }

    /**求所有的名字*/
    static void a10(){
        Set collect = getData().stream().collect(Collectors.mapping(Achievement::getName, Collectors.toSet()));
        System.out.println(collect);
    }

    /**求及格的数据*/
    static void a11(){
        Map> collect = getData().stream().collect(Collectors.partitioningBy(a -> a.getScore() >= 60));
        System.out.println(collect);
    }

    /**斐波那契数列*/
    static void b1(){
        List collect = Stream.iterate(new Integer[]{0, 1}, a -> new Integer[]{a[1], a[0] + a[1]}).limit(5).collect(Collectors.toList());
        collect.stream().forEach(a->Stream.of(a).forEach(System.out::println));
    }

    /**寻找勾股数*/
    static void b2(){
        List collect = IntStream.rangeClosed(1, 100).boxed().flatMap(x -> IntStream.rangeClosed(x, 100).filter(y -> Math.sqrt(x * x + y * y) % 1 == 0).mapToObj(y -> new Integer[]{x, y, (int) Math.sqrt(x * x + y * y)})).collect(Collectors.toList());
        collect.forEach(a->Arrays.asList(a).stream().forEach(System.out::println));
    }

你可能感兴趣的:(2019-04-25——Java8 Stream常用函数)