检索Multimap中的特定值 [英] Retrieving specific values in Multimap

查看:74
本文介绍了检索Multimap中的特定值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用每个键有两个值的Multimap.下面是我用来分别获取每个值的代码:

I'm using a Multimap that has two values per key. Below is the code I'm using to get each value separately:

代码的第一位获取第一个对象值:

The first bit of code gets the first object value:

for(Object object : map.get(object))
{
    return object
}

然后,我正在使用另一种方法来检索其他值.此方法将第一个对象作为参数:

Then, I'm using another method to retrieve the other value. This method takes the first object as an argument:

for(Object object : team.get(object))
{
    if(object != initialObject)
    {
        return object;
    }
}

这似乎是一种骇人听闻的"做事方式,那么我有什么办法可以更轻松地获取价值?

This seems like a 'hackish' way of doing things, so is there any way for me to get the values more easily?

推荐答案

Collection<Object> values = map.get(key);
checkState(values.size() == 2, String.format("Found %d values for key %s", values.size(), key));

return values.iterator().next(); // to get the first

Iterator<Object> it = values.iterator();
it.next(); // move the pointer to the second object
return it.next(); // get the second object

这篇关于检索Multimap中的特定值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆