Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does HashMap provide a one to one correspondence?

Tags:

java

hashmap

map

I found the following statement:

Map is an object that stores key/volume pairs. Given a key, you can find its value. Keys must be unique, but values may be duplicated.

For example I have just one key/value pair (3,4). And now I put a new pair (3,5). It will remove the old pair. Right? But if I put (2,4) instead of (3,4) I will add a new pair of key/value to the HashMap. Right?

like image 795
Roman Avatar asked Dec 01 '22 10:12

Roman


1 Answers

The answer to the question in your title is "No". The answer to the question in your message is "Yes". If you want a bidirectional map with unique keys and unique values as asked in your title (also called a key-key map), have a look at Guava BiMap.

like image 71
BalusC Avatar answered Dec 31 '22 12:12

BalusC