List<String> words = Arrays.asList("Hello", "World");
        List<Integer> wordLengths = words.stream()
                .map(String::length)
                .collect(toList());
        System.out.println(wordLengths);

        // flatMap
        words.stream()
                .flatMap((String line) -> Arrays.stream(line.split("")))
                .distinct()
                .forEach(System.out::println);

        words =words.stream().map(line->line.split("")).flatMap(Arrays::stream).collect(toList());
        System.out.println(words);

结果:

扁平化流flatMap

flatMap过程:

扁平化流flatMap

flatmap用途:

一言以蔽之,flatmap方法让你把一个流中的每个值都换成另一个流,然后把所有的流连接 

相关文章: