Guava学习笔记之Maps(1):Maps.uniqueIndex(Iterable, Function)
Guava官方文档 https://github.com/google/guava/wiki/CollectionUtilitiesExplained
官方文档这样描述:
[`Maps.uniqueIndex(Iterable, Function)`](http://google.github.io/guava/releases/snapshot/api/docs/com/google/common/collect/Maps.html#uniqueIndex-java.lang.Iterable-com.google.common.base.Function-) addresses the common case of having a bunch of objects that each have some unique attribute, and wanting to be able to look up those objects based on that attribute.
大概意思:描述了这样一种常见情况:有一大堆对象,每个对象都有一些独特的属性,能够根据该独特属性查找到对应对象。
Demo1:
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import java.util.ArrayList;
import java.util.function.Function;
/**
* @author: lishuai
* @date: 2018/12/4 10:20
* @description
*/
public class Application {
public static void main(String[] args) {
// nickname属性能唯一确定一个WebUser
ArrayList<WebUser> users = Lists.newArrayList(new WebUser(1,"one"),new WebUser(2,"two"),new WebUser(1,"three"),new WebUser(1,"four"));
// 得到以nickname为key,WebUser为值的一个map
ImmutableMap<String, WebUser> map = Maps.uniqueIndex(users,new com.google.common.base.Function<WebUser, String>() {
@Override
public String apply(WebUser user) {
return user.getNickname();
}
});
System.err.println("map:" + map);
System.err.println("name:" + map.get("two").getNickname());
}
}
class WebUser {
private Integer sid;
private String nickname;
public WebUser(Integer sid, String nickname) {
this.sid = sid;
this.nickname = nickname;
}
@Override
public String toString() {
return "WebUser{" +
"sid=" + sid +
", nickname='" + nickname + '\'' +
'}';
}
// set、get省略
}
可以进一步简化:
ImmutableMap<String, WebUser> map = Maps.uniqueIndex(users,WebUser::getNickname);
结果:
map:{one=WebUser{sid=1, nickname='one'}, two=WebUser{sid=2, nickname='two'}, three=WebUser{sid=1, nickname='three'}, four=WebUser{sid=1, nickname='four'}}
name:two
Demo2:
// nickname属性不能唯一确定一个WebUser(有两个元素的nickname是"one")
ArrayList<WebUser> users = Lists.newArrayList(new WebUser(1,"one"),new WebUser(2,"one"),new WebUser(1,"three"),new WebUser(1,"four"));
结果:
Exception in thread "main" java.lang.IllegalArgumentException: Multiple entries with same key: one=WebUser{sid=2, nickname='one'} and one=WebUser{sid=1, nickname='one'}. To index multiple values under a key, use Multimaps.index.
由此可见,必须确保key的唯一性。
Guava学习笔记之Maps(1):Maps.uniqueIndex(Iterable, Function)的更多相关文章
- guava 学习笔记(二) 瓜娃(guava)的API快速熟悉使用
guava 学习笔记(二) 瓜娃(guava)的API快速熟悉使用 1,大纲 让我们来熟悉瓜娃,并体验下它的一些API,分成如下几个部分: Introduction Guava Collection ...
- Guava学习笔记目录
Guava 是一个 Google 的基于java1.6的类库集合的扩展项目,包括 collections, caching, primitives support, concurrency libra ...
- guava 学习笔记 使用瓜娃(guava)的选择和预判断使代码变得简洁
guava 学习笔记 使用瓜娃(guava)的选择和预判断使代码变得简洁 1,本文翻译自 http://eclipsesource.com/blogs/2012/06/06/cleaner-code- ...
- [Guava学习笔记]Collections: 集合工具类
我的技术博客经常被流氓网站恶意爬取转载.请移步原文:http://www.cnblogs.com/hamhog/p/3861431.html,享受整齐的排版.有效的链接.正确的代码缩进.更好的阅读体验 ...
- Guava学习笔记(一)概览
Guava是谷歌开源的一套Java开发类库,以简洁的编程风格著称,提供了很多实用的工具类, 在之前的工作中应用过Collections API和Guava提供的Cache,不过对Guava没有一个系统 ...
- Guava学习笔记:Google Guava 类库简介
http://www.cnblogs.com/peida/tag/Guava/ Guava 是一个 Google 的基于java1.6的类库集合的扩展项目,包括 collections, cachin ...
- Guava学习笔记:Guava新增集合类型-Bimap
BiMap提供了一种新的集合类型,它提供了key和value的双向关联的数据结构. 通常情况下,我们在使用Java的Map时,往往是通过key来查找value的,但是如果出现下面一种场景的情况,我们就 ...
- Google Guava学习笔记——基础工具类Joiner的使用
Guava 中有一些基础的工具类,如下所列: 1,Joiner 类:根据给定的分隔符把字符串连接到一起.MapJoiner 执行相同的操作,但是针对 Map 的 key 和 value. 2,Spli ...
- Guava学习笔记:EventBus(转)
EventBus是Guava的事件处理机制,是设计模式中的观察者模式(生产/消费者编程模型)的优雅实现.对于事件监听和发布订阅模式,EventBus是一个非常优雅和简单解决方案,我们不用创建复杂的类和 ...
随机推荐
- Sublime Text几款常用插件及用法(前端)
一.Sublime3下载 百度搜索sublime text3,出现如图: 2.然后点击进去下载: 或者https://pc.qq.com/detail/0/detail_10140.html这里下载 ...
- WEB H5 JS QRCode二维码快速自动生成
万能的GITHUB: https://github.com/davidshimjs/qrcodejs HTML: <div class="col-xs-10 col-xs-offset ...
- HDU 3007 模拟退火算法
Buried memory Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Tot ...
- php扩展memcache和memcached区别?以及memcached软件的介绍
引用“http://www.vicenteforever.com/2012/03/memcache-different-memcached/” memcached是一个软件,而PHP包括了memcac ...
- jquery 通过attr获取属性只有一个值的解决
var a = []; $($('select.list[data-target="' + target + '"]').find("option:selected&qu ...
- python自学之第一章 —— 变量
1.变量的命名(): (1).可以包含数字.字母.下划线‘_’,但只能以字母和下划线‘_’开头,不能以数字开头! (2).变量的命名不能包含空格. (3).不能将python中的关键字(reserve ...
- spring中scope的prototype与singleton区别
最近在研究单例模式,突然想起项目中以下配置,scope="singleton" 和 scope="prototype"到底有何区别呢?以下做下简要分析. < ...
- 编程开发之--java多线程学习总结(4)
3.使用锁机制lock,unlock package com.lfy.ThreadsSynchronize; import java.util.concurrent.locks.Lock; impor ...
- 汉诺塔的python 动画演示
1.简介 古代有一座汉诺塔,塔内有3个座A.B.C,A座上有n个盘子,盘子大小不等,大的在下,小的在上,如图所示.有一个和尚想把这n个盘子从A座移到C座,但每次只能移动一个盘子,并且自移动过程中,3个 ...
- order by关键字
作用:用于对查询结果进行排序 select * from emp where deptno = 20 order by sal 1.如何决定升序还是降序 默认排序:升序:select * from e ...