加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

从Java Stream提取流图>

发布时间:2020-12-14 06:05:44 所属栏目:Java 来源:网络整理
导读:我有流的流(这种格式不是我设置,不能改变).例如 StreamString doc1 = Stream.of("how","are","you","doing","doing");StreamString doc2 = Stream.of("what","what","upto");StreamString doc3 = Stream.of("how","how");StreamStreamString docs = Stream.o
我有流的流(这种格式不是我设置,不能改变).例如
Stream<String> doc1 = Stream.of("how","are","you","doing","doing");
Stream<String> doc2 = Stream.of("what","what","upto");
Stream<String> doc3 = Stream.of("how","how");
Stream<Stream<String>> docs = Stream.of(doc1,doc2,doc3);

我试图将其转换为Map< String,Multiset< Integer>>的结构. (或其对应的流,因为我想进一步处理),其中密钥字符串是单词本身,并且Multiset< Integer>表示每个文档中出现的字数(应排除0). Multiset是一个谷歌番石榴类(不是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}

在Java 8中做什么好的方法?

我尝试使用flatMap,但内部Stream极大地限制了我的选择.

解决方法

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,you=[1,1]}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读