So I have a ListMultimap<Integer, String> that I want to convert to a List<Integer> that contains duplicates of the same Integer if there was >1 value associated with a Integer key in the ListMultimap. For instance, if:

ListMultimap<Integer, String> myMap = {2 -> "foo", 3 -> ("bar1, bar2")}

I want my resultant List<Integer> to look like: [2, 3, 3]. What is the easiest way to do this?

Thanks.

有帮助吗?

解决方案

Assuming this is Guava, this is just the one line

Lists.newArrayList(multimap.keys())

or, if you have an ImmutableListMultimap,

multimap.keys().asList()

(Note here that Multimap.keys() is a Multiset<Integer>, which iterates over elements exactly in the way you want -- that is, it will have one occurrence of each key for each value associated with that key.)

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top