Suppose this is my example data which are in Multimap collection:
x -> [1,2]
y -> [1,3]
z -> [4]
then I made a list of values which might be like this:(First column is index of values , second column is value)
0 -> 1
1 -> 2
2 -> 1
3 -> 3
4 -> 4
My question is how to get paired key of a value by knowing index of that value. For example the key for index "2" , the key "y" must return.
If you want to do this one time, you can do simple loop over Multimap#entries()
and maintain counter yourself, but:
Mutlimap
implementations could be different Multimap
here at all, but maintain a list (probably List<Entry<String, Integer>>
). If you really have to have multimap and it's immutable, you could use .entries().asList()
view to achieve what I described above:
//given
ImmutableListMultimap<String, Integer> multimap =
ImmutableListMultimap.<String, Integer>builder()
.putAll("x", 1, 2)
.putAll("y", 1, 3)
.putAll("z", 4)
.build();
ImmutableList<Map.Entry<String, Integer>> entriesWithPosition =
multimap.entries().asList();
//when
Map.Entry<String, Integer> foundEntry = entriesWithPosition.get(2);
//then
assertThat(foundEntry.getKey()).isEqualTo("y");
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.