Ano*_*oop 11 java java-8 java-stream
我有流的字流(这种格式不是由我设定的,不能更改).对于前者
Stream<String> doc1 = Stream.of("how", "are", "you", "doing", "doing", "doing");
Stream<String> doc2 = Stream.of("what", "what", "you", "upto");
Stream<String> doc3 = Stream.of("how", "are", "what", "how");
Stream<Stream<String>> docs = Stream.of(doc1, doc2, doc3);
Run Code Online (Sandbox Code Playgroud)
我试图把它变成一个结构Map<String, Multiset<Integer>>(或者它的相应流,因为我想进一步处理它),其中键String是单词本身,Multiset<Integer>代表每个文档中出现的单词的数量(应该排除0) .Multiset是google guava类(不是来自java.util.).
例如:
how -> {1, 2} // because it appears once in doc1, twice in doc3 and none in doc2(so doc2's count should not be included)
are -> {1, 1} // once in doc1 and once in doc3
you -> {1, 1} // once in doc1 and once in doc2
doing -> {3} // thrice in doc3, none in others
what -> {2,1} // so on
upto -> {1}
Run Code Online (Sandbox Code Playgroud)
在Java 8中执行此操作的好方法是什么?
我尝试使用flatMap,但内部Stream大大限制了我的选项.
Eug*_*ene 10
Map<String, List<Long>> map = docs.flatMap(
inner -> inner.collect(
Collectors.groupingBy(Function.identity(), Collectors.counting()))
.entrySet()
.stream())
.collect(Collectors.groupingBy(
Entry::getKey,
Collectors.mapping(Entry::getValue, Collectors.toList())));
System.out.println(map);
// {upto=[1], how=[1, 2], doing=[3], what=[2, 1], are=[1, 1], you=[1, 1]}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
673 次 |
| 最近记录: |