Multiset

Multiset 虽然带了个set但是却允许重复元素,与set相同的是不保证元素顺序。

使用场景:获取文章中单词的出现次数

        String[] wordArr = new String[]{"a","b","c","a","a","c","e"};
List<String> wordsList = Arrays.asList(wordArr);
Multiset<String> wordsMultiset = HashMultiset.create();
wordsMultiset.addAll(wordsList); for(String key:wordsMultiset.elementSet()){
System.out.println(key+" count:"+wordsMultiset.count(key));
}
//e count:1
//b count:1
//c count:2
//a count:3

Multimap

Multiset 实现了类似 Map<K, List> 的数据结构

        Multimap<String, Integer> multimap = ArrayListMultimap.create();
for (int i = 0; i < 10; i++) {
Random random = new Random();
multimap.put("1", random.nextInt());
}
System.out.println(multimap.size()); //10
System.out.println(multimap.keys()); //[1 x 10]
for (int i = 0; i < 5; i++) {
Random random = new Random();
multimap.put("2", random.nextInt());
}
System.out.println(multimap.size()); //15
System.out.println(multimap.keys()); //[2 x 5, 1 x 10]
for (String x : multimap.keySet()) {
System.out.println(x+" : ");
Map<String, Collection<Integer>> listMap = multimap.asMap();
Collection<Integer> collection = listMap.get(x);
for (Integer i : collection) {
System.out.println(i);
}
}

BiMap

BiMap提供了一种新的集合类型,它提供了key和value的双向关联的数据结构。 BiMap 要求Value是唯一的,Value重复会抛出错误

        BiMap<Integer,String> bimap = HashBiMap.create();
bimap.put(1,"1");
bimap.put(2,"2");
bimap.put(3,"3");
bimap.put(2,null); BiMap<String,Integer> valueMap = bimap.inverse();
System.out.println(valueMap.get("3")); //3 bimap.forcePut(4,"1"); //强制插入会覆盖重复Value的Key
System.out.println(valueMap.get("1")); //4

Table

Table 实现了类似Map<rowId, Map<columId, Value>> 及 可以 通过row 来查找也可以通过 colum来查找

        Table<Integer, String,Integer> table = HashBasedTable.create();
table.put(1,"lilei",23);
table.put(2,"hanmeimei",24);
table.put(3,"lilei",3);
table.put(3,"lily",18); Map<String, Integer> row = table.row(3);
System.out.println(row); //{lily=18, lilei=3}
Map<Integer, Integer> column = table.column("lilei");
System.out.println(column); //{1=23, 3=3}

RangeSet

RangeSet描述了一组不相连的、非空的区间。当把一个区间添加到可变的RangeSet时,所有相连的区间会被合并,空区间会被忽略。

    RangeSet<Integer> rangeSet = TreeRangeSet.create();
rangeSet.add(Range.closed(1, 5));
System.out.println(rangeSet); //[[1‥5]] rangeSet.add(Range.closedOpen(5, 10));
System.out.println(rangeSet); //[[1‥10)] rangeSet.add(Range.closedOpen(8, 10));
System.out.println(rangeSet); //[[1‥10)] rangeSet.add(Range.openClosed(10, 15));
System.out.println(rangeSet); //[[1‥10), (10‥15]] rangeSet.remove(Range.open(8, 12));
System.out.println(rangeSet); //[[1‥8], [12‥15]]

Google Guava--Guava新增集合的更多相关文章

  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. Google的Guava类库简介(转)

    说明:信息虽然有点旧,至少可以先了解个大概. Guava是一个Google的基于Java的类库集合的扩展项目,包括collections, caching, primitives support, c ...

  5. SpringBoot 遇到 com.google.guava » guava 组件运行异常问题修复方案

    环境 Apache Maven : 3.5.4 org.springframework.boot » spring-boot-starter-parent : 2.0.3.RELEASE io.spr ...

  6. Google的Guava之IO升华

    版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/luo201227/article/details/36413279 程序员在开发过程中,使用文件的几 ...

  7. [Google Guava]学习--新集合类型Multiset

    Guava提供了一个新集合类型Multiset,它可以多次添加相等的元素,且和元素顺序无关.Multiset继承于JDK的Cllection接口,而不是Set接口. Multiset主要方法介绍: a ...

  8. [Google Guava] 强大的集合工具类:java.util.Collections中未包含的集合工具

    转载的,有问题请联系我 原文链接 译文链接 译者:沈义扬,校对:丁一 尚未完成: Queues, Tables工具类 任何对JDK集合框架有经验的程序员都熟悉和喜欢java.util.Collecti ...

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

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

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

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

随机推荐

  1. (线段树) I Hate It --hdu--1754 (入门)

    链接: http://acm.hdu.edu.cn/showproblem.php?pid=1754 这次的代码和上个代码很相似,只不过上次的节点里存的是sum值,这次节点里存放的是Max, 正在慢慢 ...

  2. Android:手把手教你打造可缩放移动的ImageView(上)

    定义ImageView,实现功能如下: 1.初始化时图片垂直居中显示,拉伸图片宽度至ImageView宽度. 2.使用两根手指放大缩小图片,可设置最大放大倍数,当图片小于ImageView宽度时,在手 ...

  3. Codeforces 632D Longest Subsequence 2016-09-28 21:29 37人阅读 评论(0) 收藏

    D. Longest Subsequence time limit per test 2 seconds memory limit per test 256 megabytes input stand ...

  4. opencv学习_4(opencv基础数据结构 CvPoint & CvSize & CvRect & CvScalar & CvArr & CvMat)

    1:包含在cxcore/include/cxtypes.h头文件中. 2:CvPoint系列   -----(x,y) CvPoint:表示图像中的点 CvPoint2D32f:二维空间中的点 CvP ...

  5. SQL 数据库开发一些精典的代码(转自 咏南工作室)

    1.按姓氏笔画排序: Select * From TableName Order By CustomerName Collate Chinese_PRC_Stroke_ci_as 2.数据库加密: s ...

  6. The transaction associated with this command is not the connection's active transaction

    The fix is fairly simple: if you want a Dapper query to participate in a connection, explicitly deno ...

  7. 基于Quartz.net的远程任务管理系统 一

    在上一篇绪中,已经介绍了整个项目的情况下了,接下来就是开始一步步做起来了. 首先:先整个我们的Job任务表,以及Job执行日志表.SQL如下: drop table if exists job_inf ...

  8. C# RSA加解密和MD5加密

    1.RSA加密 /// <summary> /// 加密处理 /// </summary> /// <param name="content"> ...

  9. UWP开发入门(六)——对多设备不同分辨率显示效果的讨论

    本篇不涉及具体代码,而是把实际开发UWP APP的过程中,遇到的不同设备,不同分辨率显示效果差异的问题进行讨论.希望能够抛砖引玉,和各位擦出一些火花. 蜀黍我目前是在做一套牛逼的UWP APP啦,目标 ...

  10. docker-compose批量管理docker容器

    # docker-compose编排工具 #批量管理(构建.启动容器) #centos7环境准备#安装docker-ce #安装docker-compose v1. sudo curl -o /usr ...