Ashutosh Jha
Ashutosh Jha

Reputation: 1513

Guava Mutlimap to HashMap

is it possible in any way to cast Multimap to HashMap. I know we can convert multimap to map, but i want to convert multimap to hashmap. I don't think its possible but if there is any way out, please let me know.

Upvotes: 1

Views: 1334

Answers (1)

Thomas
Thomas

Reputation: 88707

is it possible in any way to cast Multimap to HashMap.

No, you can't cast a Multimap to a HashMap since - as the names indicate - they are different things, i.e. a Multimap is meant to map multiple values to a key while a Map is meant to map one value to a key. Hence converting a Multimap<K, V> to a Map would always yield something like Map<K, Collection<V>> (or, depending in the actual Multimap implementation and by using some other method a Map<K, List<V>> etc.).

I know we can convert multimap to map, but i want to convert multimap to hashmap.

You're probably referring to the asMap() method which returns a Map<K, Collection<V>>. Those probably aren't instances of HashMap but you can easily create one by calling Maps.newHashMap( multimap.asMap() ) which basically takes the generated map and copies the values (references) to a new HashMap.

Upvotes: 4

Related Questions