Guava - Set集合

当我们在统计一个字符串中每个单词出现的次数时,通常的做法是分割字符串,遍历字符串,然后放到一个map里面,来进行统计,Guava中提供了类似功能的集合,Multiset

        String strWorld="wer dffd ddsa dfd dreg de dr ce ghrt cf gt ser tg ghrt cf gt " +
                "ser tg gt kldf dfg vcd fg gt ls lser dfr wer dffd ddsa dfd dreg de dr " +
                "ce ghrt cf gt ser tg gt kldf dfg vcd fg gt ls lser dfr";
        List<String> stringList = Splitter.on(" ")
                .trimResults()
                .splitToList(strWorld);//把字符串转换为集合
        HashMultiset<String> multisets = HashMultiset.create();//创建一个Multiset集合
        multisets.addAll(stringList);
        Iterator<String> iterator = multisets.iterator();
        while (iterator.hasNext()){
            String next = iterator.next();
            System.out.println(next+" count: "+multisets.count(next));
        }

代码如此简洁清晰。

实现逻辑

内部使用Map进行实现,
HashMultiset.create()

public final class HashMultiset<E> extends AbstractMapBasedMultiset<E>
   public static <E> HashMultiset<E> create() {
        return new HashMultiset();
    }
    private HashMultiset() {
        super(new HashMap());
    }
    ... ...
 }
abstract class AbstractMultiset<E> extends AbstractCollection<E> implements Multiset<E> {
    public boolean add(@Nullable E element) {
        this.add(element, 1);
        return true;
    }
}

AbstractMapBasedMultiset

abstract class AbstractMapBasedMultiset<E> extends AbstractMultiset<E> implements Serializable {
    private transient Map<E, Count> backingMap;
    private transient long size;
    @GwtIncompatible
    private static final long serialVersionUID = -2250766705698539974L;

    protected AbstractMapBasedMultiset(Map<E, Count> backingMap) {
        this.backingMap = (Map)Preconditions.checkNotNull(backingMap);
        this.size = (long)super.size();
    }
    public int add(@Nullable E element, int occurrences) {
        if(occurrences == 0) {
            return this.count(element);
        } else {
            Preconditions.checkArgument(occurrences > 0, "occurrences cannot be negative: %s", occurrences);
            Count frequency = (Count)this.backingMap.get(element);
            int oldCount;
            if(frequency == null) {
                oldCount = 0;
                this.backingMap.put(element, new Count(occurrences));
            } else {
                oldCount = frequency.get();
                long newCount = (long)oldCount + (long)occurrences;
                Preconditions.checkArgument(newCount <= 2147483647L, "too many occurrences: %s", newCount);
                frequency.add(occurrences);
            }

            this.size += (long)occurrences;
            return oldCount;
        }
    }
    ... ...
}

转载于:https://www.cnblogs.com/hitechr/p/10473711.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值