Java8 Stream

        // 1、排除空串
        List<String> strings = Arrays.asList("abc", "", "bc", "efg", "abcd","", "jkl");
        List<String> filtered = strings.stream().filter(string -> !string.isEmpty()).collect(Collectors.toList());

        // 2、forEach

        filtered.forEach(s -> System.out.println(s)); // ==>filtered.forEach(System.out::println);

        // 3、map

        List<String> testMap = filtered.stream().map(s -> s + s).collect(Collectors.toList());

        // 4、filter

        Long count = strings.stream().filter(s -> s.isEmpty()).count();

        System.out.println("空串的数量为:count = " + count);

        // 5、limit

        filtered.stream().limit(2).forEach(System.out::println);


        // 6、sorted
        System.out.print("6 ==>");
        filtered.stream().sorted(Comparator.comparing(String::toString).reversed()).forEach(System.out::println);
        System.out.print("7 ==>");

        // 7、Collectors

        String mergeString = filtered.parallelStream().collect(Collectors.joining(","));

        System.out.println(mergeString);

        //8 、 mapToInt
        OptionalInt max = filtered.stream().mapToInt(s -> Integer.parseInt(s)).max();

 

posted @ 2019-04-04 10:02  han_shan_chang  阅读(215)  评论(0编辑  收藏  举报