Google Guava--Guava新增集合
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新增集合的更多相关文章
- Guava新增集合类型-Bimap
Guava新增集合类型-Bimap BiMap提供了一种新的集合类型,它提供了key和value的双向关联的数据结构. 通常情况下,我们在使用Java的Map时,往往是通过key来查找value的,但 ...
- Guava新增集合类型-Multimap
Guava新增集合类型-Multimap 在日常的开发工作中,我们有的时候需要构造像Map<K, List<V>>或者Map<K, Set<V>>这样比 ...
- Guava新增集合类型-Multiset
Guava新增集合类型-Multiset Guava引进了JDK里没有的,但是非常有用的一些新的集合类型.所有这些新集合类型都能和JDK里的集合平滑集成.Guava集合非常精准地实现了JDK定义的接口 ...
- Google的Guava类库简介(转)
说明:信息虽然有点旧,至少可以先了解个大概. Guava是一个Google的基于Java的类库集合的扩展项目,包括collections, caching, primitives support, c ...
- SpringBoot 遇到 com.google.guava » guava 组件运行异常问题修复方案
环境 Apache Maven : 3.5.4 org.springframework.boot » spring-boot-starter-parent : 2.0.3.RELEASE io.spr ...
- Google的Guava之IO升华
版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/luo201227/article/details/36413279 程序员在开发过程中,使用文件的几 ...
- [Google Guava]学习--新集合类型Multiset
Guava提供了一个新集合类型Multiset,它可以多次添加相等的元素,且和元素顺序无关.Multiset继承于JDK的Cllection接口,而不是Set接口. Multiset主要方法介绍: a ...
- [Google Guava] 强大的集合工具类:java.util.Collections中未包含的集合工具
转载的,有问题请联系我 原文链接 译文链接 译者:沈义扬,校对:丁一 尚未完成: Queues, Tables工具类 任何对JDK集合框架有经验的程序员都熟悉和喜欢java.util.Collecti ...
- Guava学习笔记:Guava新增集合类型-Bimap
BiMap提供了一种新的集合类型,它提供了key和value的双向关联的数据结构. 通常情况下,我们在使用Java的Map时,往往是通过key来查找value的,但是如果出现下面一种场景的情况,我们就 ...
- Guava学习笔记:Guava新增集合类型-Multimap
在日常的开发工作中,我们有的时候需要构造像Map<K, List<V>>或者Map<K, Set<V>>这样比较复杂的集合类型的数据结构,以便做相应的业 ...
随机推荐
- (并查集) Wireless Network --POJ --2236
链接: http://poj.org/problem?id=2236 http://acm.hust.edu.cn/vjudge/contest/view.action?cid=82830#probl ...
- visual studio 2015 rc &cordova -hello world
初始环境,用来看看书,电影,上上网的win8,所以一切从头开始. 1,首先还是装visual studio 2015 rc吧,目前只放出在线安装,所以要很长很长时间.不过有新闻说很快要实现中国网友至 ...
- Delphi Language Overview
Delphi is a high-level, compiled, strongly typed language that supports structured and object-orient ...
- [微信开发] 微信网页授权Java实现
功能:主要用于在用户通过手机端微信访问第三方H5页面时获取用户的身份信息(openId,昵称,头像,所在地等..)可用来实现微信登录.微信账号绑定.用户身份鉴权等功能. 开发前的准备: 1.需 ...
- Asp.Net从相对路径获取绝对路径的方法(不需要httpcontext上下文也可)
//如果拿不到当前HttpContext上下文的话可以用该方法取得绝对路径 var filePath = HostingEnvironment.MapPath("需要获取绝对路径 的 相对路 ...
- The Beam Model:Stream & Tables翻译(上)
作者:周思华 欢迎访问网易云社区,了解更多网易技术产品运营经验. 本文尝试描述Beam模型和Stream & Table理论间的关系(前者描述于数据流模型论文.the-world-beyond ...
- pageadmin CMS网站建设教程:如何修改用户密码?
pageadmin CMS网站建设教程: 当我们想修改密码,该如何修改呢? 1. 首先,登录会员中心,会员中心的地址是在网址后面加上/member/login: 2. 例:我的网站地址是localho ...
- day 93 Restframwork
苑昊博客: http://www.cnblogs.com/yuanchenqi/articles/7570003.html 一.queryset 特性 from django.db import m ...
- kernel 调试 打印IP地址
#define NIPQUAD(addr) \ ((unsigned char *)&addr)[0], \ ((unsigned char *)&addr)[1], \ ((unsi ...
- leetcode 121. 买卖股票的最佳时机 JAVA
题目: 给定一个数组,它的第 i 个元素是一支给定股票第 i 天的价格. 如果你最多只允许完成一笔交易(即买入和卖出一支股票),设计一个算法来计算你所能获取的最大利润. 注意你不能在买入股票前卖出股票 ...