当我们在统计一个字符串中每个单词出现的次数时,通常的做法是分割字符串,遍历字符串,然后放到一个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;
}
}
... ...
}

Guava - Set集合的更多相关文章

  1. Guava新增集合类型-Bimap

    Guava新增集合类型-Bimap BiMap提供了一种新的集合类型,它提供了key和value的双向关联的数据结构. 通常情况下,我们在使用Java的Map时,往往是通过key来查找value的,但 ...

  2. Guava新增集合类型-Multimap

    Guava新增集合类型-Multimap 在日常的开发工作中,我们有的时候需要构造像Map<K, List<V>>或者Map<K, Set<V>>这样比 ...

  3. Guava新增集合类型-Multiset

    Guava新增集合类型-Multiset Guava引进了JDK里没有的,但是非常有用的一些新的集合类型.所有这些新集合类型都能和JDK里的集合平滑集成.Guava集合非常精准地实现了JDK定义的接口 ...

  4. Guava学习笔记:Guava新集合-Table等

    Table 当我们需要多个索引的数据结构的时候,通常情况下,我们只能用这种丑陋的Map<FirstName, Map<LastName, Person>>来实现.为此Guava ...

  5. Guava学习笔记:Guava新增集合类型-Bimap

    BiMap提供了一种新的集合类型,它提供了key和value的双向关联的数据结构. 通常情况下,我们在使用Java的Map时,往往是通过key来查找value的,但是如果出现下面一种场景的情况,我们就 ...

  6. Guava学习笔记:Guava新增集合类型-Multimap

    在日常的开发工作中,我们有的时候需要构造像Map<K, List<V>>或者Map<K, Set<V>>这样比较复杂的集合类型的数据结构,以便做相应的业 ...

  7. Guava学习笔记:Guava新增集合类型-Multiset

    Guava引进了JDK里没有的,但是非常有用的一些新的集合类型.所有这些新集合类型都能和JDK里的集合平滑集成.Guava集合非常精准地实现了JDK定义的接口.Guava中定义的新集合有: Multi ...

  8. guava学习--集合2&Range

    转载:http://www.cnblogs.com/peida/p/Guava_ImmutableCollections.html Table: 当我们需要多个索引的数据结构的时候,通常情况下,我们只 ...

  9. Guava 3: 集合Collections

    一.引子 Guava 对JDK集合的拓展,是最成熟且最受欢迎的部分.本文属于Guava的核心,需要仔细看. 二.Guava 集合 2.1 Immutable Collections不可变集合 1.作用 ...

  10. guava -- 新集合类型

    Guava引入了很多JDK没有的.但有用的新集合类型.这些新类型是为了和JDK集合框架共存,而没有往JDK集合抽象中硬塞其他概念. 作为一般规则,Guava集合非常精准地遵循了JDK接口契约. 1. ...

随机推荐

  1. 使用cookies,免密登录禅道(一)

    导言:在做自动化的过程中,很多时候都需要绕过登录验证码来进行测试,可使用cookie 绕过验证码进行登录. 以下以自己搭建的禅道环境登录为例(其他网站也可以同样道理): #coding=gbkimpo ...

  2. robotframework常用关键字

    robotframework关键字 可以将关键字看作是处理数据的方法.robotframework的关键字和测试数据组成了测试用例. robotframework关键字包括系统关键字和用户关键字.用户 ...

  3. 聊聊推荐系统,FM模型效果好在哪里?

    本文始发于公众号:Coder梁 大家好,我们今天继续来聊聊推荐系统. 在上一回当中我们讨论了LR模型对于推荐系统的应用,以及它为什么适合推荐系统,并且对它的优点以及缺点进行了分析.最后我们得出了结论, ...

  4. Redis 性能问题分析

    在一些网络服务的系统中,Redis 的性能,可能是比 MySQL 等硬盘数据库的性能更重要的课题.比如微博,把热点微博[1],最新的用户关系,都存储在 Redis 中,大量的查询击中 Redis,而不 ...

  5. 『心善渊』Selenium3.0基础 — 2、Selenium测试框架环境搭建(Windows)

    目录 1.浏览器安装 2.浏览器驱动下载 (1)ChromeDriver for Chrome (2)Geckodriver for Firefox (3)IEDriverServer for IE ...

  6. 使用axios模拟表单提交

    1.需求背景 最近在实验室写一个Spring前后端分离的项目,项目中使用Spring Security组件实现系统的认证和授权,当Security的认证模式设置为FormLogin时(如下代码),前端 ...

  7. Springboot WebFlux集成Spring Security实现JWT认证

    我最新最全的文章都在南瓜慢说 www.pkslow.com,欢迎大家来喝茶! 1 简介 在之前的文章<Springboot集成Spring Security实现JWT认证>讲解了如何在传统 ...

  8. kubelet分析-csi driver注册源码分析

    kubelet注册csi driver分析 kubelet注册csi driver的相关功能代码与kubelet的pluginManager有关,所以接下来对pluginManager进行分析.分析将 ...

  9. 8、mysql乱码问题及字符集实战

    8.1.mysql插入中文数据乱码案例: mysql建库的字符集为latin1,客户端的字符集为utf8; use lc; 1.查看库的编码: mysql> show create databa ...

  10. UVA 10689 Yet another Number Sequence 矩阵快速幂 水呀水

    #include <iostream> #include <cstdio> #include <cstring> #include <algorithm> ...