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的唯一性。

转载于:https://www.cnblogs.com/pianistedward/p/10170828.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值