Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Map kind of behavior needed

Tags:

java

map

In map we have key, value pair.If we try to put same key in map 2 times it will produce a error.Now i want similar kind of behavior for value too.In short when i put a new key,value into the map both key and value should be unique, or else it should through an exception, How can i achive this?

like image 845
akshay Avatar asked Dec 22 '22 00:12

akshay


1 Answers

Sounds like you want a Guava Bimap.

A bimap (or "bidirectional map") is a map that preserves the uniqueness of its values as well as that of its keys. This constraint enables bimaps to support an "inverse view", which is another bimap containing the same entries as this bimap but with reversed keys and values.

(Guava is a great library in general, btw. Well worth using.)

like image 113
Jon Skeet Avatar answered Jan 08 '23 01:01

Jon Skeet