質問

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