ali*_*ind 4 java java-8 java-stream
我正在阅读文本行,并创建一个独特单词列表(在小写之后).我可以使用flatMap进行此操作,但无法使其与地图的"子"流一起使用.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) {
Run Code Online (Sandbox Code Playgroud)
为什么这个不同允许重复:
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("--------");
Run Code Online (Sandbox Code Playgroud)
输出:
Output via map:
line
1
line
2
which
is
a
really
long
a
moderately
long
line
3
line
4
Run Code Online (Sandbox Code Playgroud)
但这正确地消除了重复?
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);
}
}
Run Code Online (Sandbox Code Playgroud)
输出:
line
1
2
which
is
a
really
long
moderately
3
4
Run Code Online (Sandbox Code Playgroud)
这是完整的输出,包括下面的peeks.您可以看到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
--------
Run Code Online (Sandbox Code Playgroud)
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
Run Code Online (Sandbox Code Playgroud)
完整代码:
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);
}
}
Run Code Online (Sandbox Code Playgroud)
第一个代码片段用于forEach处理每一行,并且distinct在此范围内forEach- 因此消除了重复,但仅在一行内,而不是全局.
看到第二行的输出,实际上消除了'line'的重复出现,因为它在同一行上重复.