Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Retrieving specific values in Multimap

Tags:

java

guava

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?

like image 336
tripleblep Avatar asked Mar 04 '26 06:03

tripleblep


1 Answers

If you're using Guava, Iterables#get is probably what you want - it returns the Nth item from any iterable, example:

Multimap<String, String> myMultimap = ArrayListMultimap.create();

// and to return value from position:
return Iterables.get(myMultimap.get(key), position);

If you're using a ListMultimap then it behaves very much like a map to a List so you can directly call get(n).

like image 114
Alan Stokes Avatar answered Mar 06 '26 19:03

Alan Stokes



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!