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)的更多相关文章

  1. guava 学习笔记(二) 瓜娃(guava)的API快速熟悉使用

    guava 学习笔记(二) 瓜娃(guava)的API快速熟悉使用 1,大纲 让我们来熟悉瓜娃,并体验下它的一些API,分成如下几个部分: Introduction Guava Collection ...

  2. Guava学习笔记目录

    Guava 是一个 Google 的基于java1.6的类库集合的扩展项目,包括 collections, caching, primitives support, concurrency libra ...

  3. guava 学习笔记 使用瓜娃(guava)的选择和预判断使代码变得简洁

    guava 学习笔记 使用瓜娃(guava)的选择和预判断使代码变得简洁 1,本文翻译自 http://eclipsesource.com/blogs/2012/06/06/cleaner-code- ...

  4. [Guava学习笔记]Collections: 集合工具类

    我的技术博客经常被流氓网站恶意爬取转载.请移步原文:http://www.cnblogs.com/hamhog/p/3861431.html,享受整齐的排版.有效的链接.正确的代码缩进.更好的阅读体验 ...

  5. Guava学习笔记(一)概览

    Guava是谷歌开源的一套Java开发类库,以简洁的编程风格著称,提供了很多实用的工具类, 在之前的工作中应用过Collections API和Guava提供的Cache,不过对Guava没有一个系统 ...

  6. Guava学习笔记:Google Guava 类库简介

    http://www.cnblogs.com/peida/tag/Guava/ Guava 是一个 Google 的基于java1.6的类库集合的扩展项目,包括 collections, cachin ...

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

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

  8. Google Guava学习笔记——基础工具类Joiner的使用

    Guava 中有一些基础的工具类,如下所列: 1,Joiner 类:根据给定的分隔符把字符串连接到一起.MapJoiner 执行相同的操作,但是针对 Map 的 key 和 value. 2,Spli ...

  9. Guava学习笔记:EventBus(转)

    EventBus是Guava的事件处理机制,是设计模式中的观察者模式(生产/消费者编程模型)的优雅实现.对于事件监听和发布订阅模式,EventBus是一个非常优雅和简单解决方案,我们不用创建复杂的类和 ...

随机推荐

  1. kali linux之取证

    取证简介: CSI:物理取证 指纹.DNA.弹道.血迹 无力取证的理论基础是物质交换原则 数字取证/计算机取证 智能设备.计算机.手机平板.loT.有线及无线信道.数据存储 事件响应调查------黑 ...

  2. leecode刷题(16)-- 字符串转换整数

    leecode刷题(16)-- 字符串转换整数 字符串转换整数 描述: 请你来实现一个 atoi 函数,使其能将字符串转换成整数. 首先,该函数会根据需要丢弃无用的开头空格字符,直到寻找到第一个非空格 ...

  3. Qt5学习笔记(消息基础)

    #include "MyWidget.h" #include <QApplication> #include <QEvent> #include <Q ...

  4. [CISCO] 转载:冲突域与广播域(区别、知识要点)

    [CISCO] 转载:冲突域与广播域(区别.知识要点) 1.传统以太网操作(Ethernet Connection Ethernet) 传统共享式以太网的典型代表是总线型以太网.在这种类型的以太网中, ...

  5. Nginx+SpringBoot搭建负载均衡

    v\:* {behavior:url(#default#VML);} o\:* {behavior:url(#default#VML);} w\:* {behavior:url(#default#VM ...

  6. mybaits中"#"和"$"的区别

    动态 sql 是 mybatis 的主要特性之一,在 mapper 中定义的参数传到 xml 中之后,在查询之前 mybatis 会对其进行动态解析.mybatis 为我们提供了两种支持动态 sql ...

  7. appium安装与部署

    前提: ①:appium属于C/S架构,代码写在Client端 ②:本章所说的部署讲的是Android设备下的Appium安装与部署 ③:Appium Client的环境是针对python3的 App ...

  8. angularJs集成百度地图

    app.controller('mapSignController',function($scope,$rootScope,Message, $window,$uibModalInstance){ v ...

  9. [转] CDH6 安装文章链接收集

    CentOS 7下Cloudera Manager及CDH 6.0.1安装过程详解 http://blog.51cto.com/wzlinux/2321433?source=dra Cloudera ...

  10. javascript的简单查询和插入和修改

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/stri ...