为什么通过flatMap进行明显的工作,而不是通过map" sub-stream"?

时间:2015-10-09 20:42:38

标签: java java-8 java-stream

我正在阅读文本行,并创建一个独特单词列表(在小写之后)。我可以使用flatMap来完成这项工作,但不能使它适用于地图的" sub"流。 flatMap看起来更简洁,更好"但是为什么在一个上下文中有明显的工作而在另一个上下文中却没有?

班级顶部:

import static java.util.stream.Collectors.toList;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.regex.Pattern;

public class GetListOfAllWordsInLinesOfText {

   private static final String INPUT = "Line 1\n" +
                              "Line 2, which is a really long line\n" +
                              "A moderately long line 3\n" +
                              "Line 4\n";
   private static final Pattern WORD_SEPARATOR_PATTERN = Pattern.compile("\\W+");

   public static void main(String[] args) {

为什么这种不同允许重复:

      final List<String> wordList = new ArrayList<>();
      Arrays.stream(INPUT.split("\n"))
            .forEach(line -> WORD_SEPARATOR_PATTERN.splitAsStream(line).
                        map(String::toLowerCase)
                        distinct().
                        forEach(wordList::add));

      System.out.println("Output via map:");
      wordList.stream().forEach(System.out::println);

      System.out.println("--------");

输出:

Output via map:
line
1
line
2
which
is
a
really
long
a
moderately
long
line
3
line
4

但是这正确地消除了重复吗?

      final List<String> wordList2 = Arrays.stream(INPUT.split("\n")).flatMap(
            WORD_SEPARATOR_PATTERN::splitAsStream).map(String::toLowerCase).
            distinct()
            .collect(toList());

      System.out.println("Output via flatMap:");
      wordList2.stream().forEach(System.out::println);
   }
}

输出:

line
1
2
which
is
a
really
long
moderately
3
4

这里是完整输出,包括以下peek s。您可以看到flatMap版本正确过滤了重复项,但不是地图版本:

图:

map before distinct -> line
map after distinct -> line
map before distinct -> 1
map after distinct -> 1
map before distinct -> line
map after distinct -> line
map before distinct -> 2
map after distinct -> 2
map before distinct -> which
map after distinct -> which
map before distinct -> is
map after distinct -> is
map before distinct -> a
map after distinct -> a
map before distinct -> really
map after distinct -> really
map before distinct -> long
map after distinct -> long
map before distinct -> line
map before distinct -> a
map after distinct -> a
map before distinct -> moderately
map after distinct -> moderately
map before distinct -> long
map after distinct -> long
map before distinct -> line
map after distinct -> line
map before distinct -> 3
map after distinct -> 3
map before distinct -> line
map after distinct -> line
map before distinct -> 4
map after distinct -> 4
Output via map:
line
1
line
2
which
is
a
really
long
a
moderately
long
line
3
line
4
--------

flatMap:

flatMap before distinct -> line
flatMap after distinct -> line
flatMap before distinct -> 1
flatMap after distinct -> 1
flatMap before distinct -> line
flatMap before distinct -> 2
flatMap after distinct -> 2
flatMap before distinct -> which
flatMap after distinct -> which
flatMap before distinct -> is
flatMap after distinct -> is
flatMap before distinct -> a
flatMap after distinct -> a
flatMap before distinct -> really
flatMap after distinct -> really
flatMap before distinct -> long
flatMap after distinct -> long
flatMap before distinct -> line
flatMap before distinct -> a
flatMap before distinct -> moderately
flatMap after distinct -> moderately
flatMap before distinct -> long
flatMap before distinct -> line
flatMap before distinct -> 3
flatMap after distinct -> 3
flatMap before distinct -> line
flatMap before distinct -> 4
flatMap after distinct -> 4
Output via flatMap:
line
1
2
which
is
a
really
long
moderately
3
4

完整代码:

import static java.util.stream.Collectors.toList;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.regex.Pattern;

public class GetListOfAllWordsInLinesOfText {

   private static final String INPUT = "Line 1\n" +
                              "Line 2, which is a really long line\n" +
                              "A moderately long line 3\n" +
                              "Line 4\n";
   private static final Pattern WORD_SEPARATOR_PATTERN = Pattern.compile("\\W+");

   public static void main(String[] args) {

      final List<String> wordList = new ArrayList<>();
      Arrays.stream(INPUT.split("\n"))
            .forEach(line -> WORD_SEPARATOR_PATTERN.splitAsStream(line).map(String::toLowerCase)
                  .peek(word -> System.out.println("map before distinct -> " + word)).
                        distinct().
                        peek(word -> System.out.println("map after distinct -> " + word)).
                        forEach(wordList::add));

      System.out.println("Output via map:");
      wordList.stream().forEach(System.out::println);

      System.out.println("--------");

      final List<String> wordList2 = Arrays.stream(INPUT.split("\n")).flatMap(
            WORD_SEPARATOR_PATTERN::splitAsStream).map(String::toLowerCase).
                  peek(word -> System.out.println("flatMap before distinct -> " + word)).
            distinct()
                  .peek(word -> System.out.println("flatMap after distinct -> " + word))
            .collect(toList());

      System.out.println("Output via flatMap:");
      wordList2.stream().forEach(System.out::println);
   }
}

0 个答案:

没有答案