收集流时如何使用Guava的Multisets.toMultiSet()? [英] How to use Guava's Multisets.toMultiSet() when collecting a stream?

查看:86
本文介绍了收集流时如何使用Guava的Multisets.toMultiSet()?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个字符串列表,其中每个字符串都由字母组成,并用字符,"(逗号)分隔. 我想遍历字符串列表,以逗号分隔,计算每个字母出现多少次,并将结果存储在Multiset中.空白字符串应被忽略,并且拆分的部分应被修剪.多重集应按键排序.

I have list of strings, where each string consists of letters separated by the character ',' (comma). I want to go through the list of strings, split on comma, and calculate how many times each letter occurs, and store the result in a Multiset. Blank strings should be ignored, and the split parts should be trimmed. The multiset should be sorted on key.

以下代码有效,即,它产生所需的多集.但是,我无法弄清楚如何使用适当的收集器方法(Multisets.toMultiset()),因此诉诸于两步解决方案,使用了一个我想消除的临时列表变量.

The below code works, i.e., it produces the desired Multiset. However, I couldn't figure out how to use the proper collector method (Multisets.toMultiset()), so resorted to a two-step solution, using a temporary list variable, which I would like to eliminate.

如果有人可以告诉我如何在收集步骤中构造对Multisets.toMultiset()的调用,我将不胜感激.我被困在定义元素功能和供应商功能上,我什至无法编写已编译的代码...

I would appreciate if someone can show me how I should have constructed the call to Multisets.toMultiset() in the collect-step. I got stuck on defining the element function and the supplier function, I couldn't even make code that compiled...

@Test
public void testIt() {
    List<String> temp = Stream.of("b, c", "a", "  ", "a, c")
            .filter(StringUtils::isNotBlank)
            .map(val -> val.split(","))
            .flatMap(Arrays::stream)
            .map(String::trim)
            .collect(Collectors.toList());

    Multiset<String> multiset = ImmutableSortedMultiset.copyOf(temp);

    System.out.println("As list: " + temp);
    System.out.println("As multiset: " + multiset);
    // Output is:
    // As list: [b, c, a, a, c]
    // As multiset: [a x 2, b, c x 2]
}

我正在使用番石榴28.1.上面的示例中还使用了commons-lang3版本3.9中的StringUtils类

I'm using Guava 28.1. Also used in the example above is the StringUtils class from commons-lang3, version 3.9

这是实际情况中的简化示例,但仍然可以捕捉到我的问题的本质

This is a simplified example from the real scenario, but one that still captures the essence of my problem

推荐答案

如果您确实想省略第二个复制阶段,则有几种方法可以实现:

If you really want to ommit the second copy stage, there are several ways to achieve this:

  1. 已经指定了ImmatbleSortedMultiset收集器

.collect(ImmutableSortedMultiset.toImmutableSortedMultiset(Comparator.naturalOrder()));

  • 因为您一直在问如何使用MultiSets::toMultiset

    .collect(Multisets.toMultiset(Function.identity(), i -> 1, TreeMultiset::create));
    

  • 或者您可以使用Builder

    .collect(Collector.of(
        ImmutableSortedMultiset::<String>naturalOrder,
        ImmutableSortedMultiset.Builder::add,
        (b1, b2) -> {b1.addAll(b2.build()); return b1;},
        ImmutableSortedMultiset.Builder::build)
    );
    

  • 这篇关于收集流时如何使用Guava的Multisets.toMultiSet()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆