java8 List 合併 reduce

        List<List<Integer>> list = new ArrayList<>();
        list.add(Arrays.asList(new Integer[]{1,2}));
        list.add(Arrays.asList(new Integer[]{3,4}));
        list.add(null);
        list.add(Arrays.asList(new Integer[]{4,3,5}));
        List<Integer> result = list.stream()
                .filter(item-> item != null)
                .reduce(new ArrayList<>(), (all, item ) -> {all.addAll(item); return all;});
        System.out.println(result);
相關文章
相關標籤/搜索