Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"Transpose" a hashmap for key->value to value->key?

Tags:

java

hashmap

Say I have a map of key -> value pairs, I want to reverse this so that I have a new map which is effectively value -> key (i.e. the old value becomes the new key and the old key becomes the new value).

Whats the best way to do this? (I am using Java...).

Oh and values are unique.

like image 504
aeq Avatar asked Nov 29 '22 04:11

aeq


1 Answers

Personally I'd use a Guava BiMap to start with (with an implementation such as HashBiMap) and then call inverse() when I wanted to use the values as keys :)

like image 90
Jon Skeet Avatar answered Dec 09 '22 15:12

Jon Skeet