我正在使用java 8的计数收集器来获取有关计数值的信息。Java8计数收集器附加信息
对于ex; 如果我有一个像
Stream<String> doc1 = Stream.of("a", "b", "c", "b", "c");
Stream<String> doc2 = Stream.of("b", "c", "d");
Stream<Stream<String>> docs = Stream.of(doc1, doc2);
我能够做
List<Map<String, Long>> collect = docs
.map(doc -> doc.collect(Collectors.groupingBy(Function.identity(), Collectors.counting())))
.collect(Collectors.toList());
这导致结构
[
{a=1, b=2, c=2},
{b=1, c=1, d=1}
]
数在一个文档中每个单词的出现一堆流但是,我想让这个计数与它所源自的docId相关联。例如,我想有一个结构
[
{a=(randId1, 1), b=(randId1, 2), c=(randId1, 2)},
{b=(randId2, 1), c=(randId2, 1), d=(randId2, 1)}
]
其中randId1
和randId2
可以在运行时产生的(I只需要一种方法来追溯到唯一的源)和()
表示Apache的一个Pair类。
我试图来包装的一个(docId, doc)
的Pair
DOC但我被困在修改Collectors.counting()
替代
List<Map<String, Long>> collect = docs.map(doc -> Pair.of(UUID.randomUUID(), doc))
.map(p -> p.getRight().collect(Collectors.groupingBy(Function.identity(), Collectors.counting())))
.collect(Collectors.toList());
如何获得所需要的格式输出?
很好的答案..谢谢 – Anoop
@Anoop一点也不。这是我的荣幸。 –