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是一个非常优雅和简单解决方案,我们不用创建复杂的类和 ...
随机推荐
- “全栈2019”Java第四章:创建第一个Java程序
难度 初级 学习时间 10分钟 适合人群 零基础 开发语言 Java 开发环境 JDK v11 IntelliJ IDEA v2018.3 文章原文链接 "全栈2019"Java第 ...
- jzoj3717
#include<bits/stdc++.h> using namespace std; extern int main2(void) __asm__ ("main2" ...
- php扩展memcache和memcached区别?以及memcached软件的介绍
引用“http://www.vicenteforever.com/2012/03/memcache-different-memcached/” memcached是一个软件,而PHP包括了memcac ...
- [HTML] <meta name="viewport" content="width=device-width,initial-scale=1.0">释义
<meta name="viewport" content="width=device-width,initial-scale=1.0">这是 HT ...
- [Objective-C语言教程]类别(28)
有时,可能会发现希望通过添加仅在某些情况下有用的行为来扩展现有类. 要向现有类添加此类扩展,Objective-C提供了类别和扩展. 如果需要向现有类添加方法,或许为了添加功能以便在应用程序中更容易地 ...
- mac安装gdb调试(转载)
转载自:http://blog.plotcup.com/a/129 最近一直用go写一个项目,本想在mac上用gdb调试一下,但xcode4.6带的gdb版 本还是太低了,不支持go,只好自己安装一个 ...
- localStorage注册页面A注册数据在本地储存并在B页面打开
如题目的这么一个问题, A页面代码 <!DOCTYPE html> <html lang="en"> <head> <meta chars ...
- idea 验证码
N757JE0KCT-eyJsaWNlbnNlSWQiOiJONzU3SkUwS0NUIiwibGljZW5zZWVOYW1lIjoid3UgYW5qdW4iLCJhc3NpZ25lZU5hbWUiO ...
- deepin安装php5.6
sudo su -echo "deb http://ppa.launchpad.net/ondrej/php/ubuntu xenial main" | tee -a /etc/a ...
- AngularJS Eclipse Plugin
本文介绍如何安装和配置 AngularJS Eclipse.AngularJS Eclipse 插件是基于强大的 JavaScript 推断引擎(javascript inference engine ...