Does HashMap provide personalized correspondence?

I found the following statement:

A map is an object that stores key / volume pairs. Given the key, you can find its value. Keys must be unique, but values ​​can be duplicated.

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

+4
source share
4 answers

Yes, the first number in parentheses is the key, you can consider it an address. The second is meaning. The key is unique, like your home address, but the value can be anything.

+7
source

The answer to the question in your title is "No." The answer to the question in your message is "Yes." If you need a bidirectional map with unique keys and unique values ​​indicated in your name (also called a key key map), see Guava BiMap .

+10
source

You're right. There is only one value for each key. But this does not mean that several keys have the same value.

Think of HashMap as a box of boxes, each box can contain only one object, and each box is marked with a key. Therefore, after you put the apple in a box with a mark of 1, you can not put it in orange if you do not pull the apple out first. However, nothing prevents you from putting oranges in boxes 2 3 and 4.

+2
source

Yes, that's right. If you need personalized correspondence, use BiMap from Guava (or the Google Java Collection Library ).

+1
source

Source: https://habr.com/ru/post/1304893/


All Articles