0

I'm trying to solve an analytic problem just for study data structures. My doubt is with the HashTable in Java.

I have a HashMap

HashMap<String, Integer> map = new HashMap<>();

And this HashMap has any fewer keys, but, some of these keys are duplicated.

map.put('k', 1);
map.put('k', 2);

And So on...

My question is when I am gonna remove a key to the HashMap. Some of these keys are duplicated too.

Let's see.

map.remove('k');

I suppose that in this case, it will remove all the values with the key 'k' or it just only remove the first it found.

What's going to happen in this case? I'm a little confused.

Thanks for your help!!

1
  • 1
    Thanks, guys. I am glad to read your answers. You all are right, it will overwrite the value. Thanks!! Commented Mar 24, 2018 at 17:18

5 Answers 5

2

In HashMap (or HashTable) you can only have UNIQUE KEYS, you cannot have different values assigned to the same key. In your code you attempt put 2 different values with the same key:

map.put('k', 1);
map.put('k', 2);

Guess what, there will be no 2 entries, but only 1, the last, which will REPLACE previous one, since they have the same key - 'k'. Hence, map.remove('k'); will remove everything which is just one single entry, not two.

Sign up to request clarification or add additional context in comments.

Comments

1

There are multiple things you are asking. Let's answer all of them.

  1. HashTable is not the same as HashMap. However, hashTable is very similar to HashMap. The biggest difference between them is that in HashTable, every method is synchronized, which makes it extremely expensive to do a read/write. HashMap's methods are not synchronized. HashTable is more or less obsolete and people writing new code should avoid using a HashTable.

  2. In a HashMap, keys are always unique. i.e., there cannot be 2 entries with the same key. In your example,

map.put('k', 1);

This will create an entry in the map whose key is 'k' and value is 1.

Then, you do

map.put('k', 2);

This will not create another entry with key 'k' and value 2. This will overwrite the value for the first entry itself. So, you will only have a singe entry for key 'k' whose value is now 2 (and not 1)

Now, I guess understanding remove() would be easy. When you do remove(key), it tries removing the only entry for that key.

Comments

0

In HashMap Keys will be unique , so it wont add multiple times of key K . when you remove key "K" , it will remove the unique key 'K' from the hashtable.

Comments

0

By definition a HashMap in Java stores all keys uniquely. Simply, when we try to put more entries with the same key it will override the previously defined value for that key. Hence, when deleting a key after putting multiple values for that key means the key will no longer exist in the HashMap.

For more details you can read the documentation here.

Comments

0

use

map.putIfAbsent('k',2);

instead of

map.put('k', 2);

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.