天下脸皮共十分
我占八分

reduce

public static void main(String[] args) {
        List<Integer> numList = new ArrayList<>();
        for (int i = 0; i < 100; i++) {
            numList.add(i + 1);
        }
        Integer reduce = numList.stream().reduce(0, (a, b) -> a + b); // 5050
        System.out.println(reduce);
        Optional<Integer> sum = numList.stream().reduce((a, b) -> {
            a += b;
            System.out.println("a = " + a);
            System.out.println("b = " + b);
            return a;
        });
        System.out.println(sum.get()); // 5050
        List<String> list = new ArrayList<>();
        List<Integer> numList2 = new ArrayList<>();
        String s = Stream.of("a", "b", "c", "d", "e").reduce("", String::concat);
//        Stream.of("a", "b", "c", "d", "e").reduce(list, (a, b) -> a.add);
        Stream.of(1, 2, 3, 4, 5).reduce(Integer::sum);
        System.out.println(s);
        String concat = "sssa".concat("sasas");
        System.out.println(concat);

        ArrayList<Integer> newList = new ArrayList<>();

        ArrayList<Integer> accResult_ = Stream.of(2, 3, 4)
                .reduce(newList,
                        (acc, item) -> {
                            acc.add(item);
                            System.out.println("item: " + item);
                            System.out.println("acc+ : " + acc);
                            System.out.println("BiFunction");
                            return acc;
                        }, (acc, item) -> null);
        System.out.println("accResult_: " + accResult_);
    }
赞(0) 打赏
未经允许不得转载:Stephen Young » reduce
分享到: 更多 (0)

评论 抢沙发

评论前必须登录!

 

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

微信扫一扫打赏