Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Deleting HashMap Key

 
henri henri
Ranch Hand
Posts: 115
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Would anyone know if deleting a HashMap key also deletes the associated value? I am still a beginner in Java, and have alot to learn. Thank you in advance.

 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Anyhow, removing an entry from the map removes one reference to the key and one reference to the value from memory. If there are now zero references to those things, they are eligible for garbage collection. Rewriting the collection to an object stream will write it without the name and value objects. Do any of those sound like what you were trying to do?
[ November 05, 2005: Message edited by: Stan James ]
 
Rob Spoor
Sheriff
Pie
Posts: 20495
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What are you doing here??
You know that in a map all keys are unique. You iterate over these keys. You then call container.get(it.next()) - you retrieve (but not remove) the value belonging to the key, and ignore it afterwards. Then you try to delete the key-value pair for the name parameter of the method.

This can all be done in only one statement:
There is no need to iterate over the entire map, and like Stan said, remove does nothing if the key is not present.


One more note: you will get ConcurrentModificationExceptions if you remove elements from a collection or map while you are iterating over them. That is exactly why Iterator has a remove() method.
The proper way to remove elements from a collection / map:
(A while loop will also do of course)
[ November 06, 2005: Message edited by: Rob Spoor ]
 
henri henri
Ranch Hand
Posts: 115
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks. I read your message Rob and I modified my class to just use container.remove(name) without Iteration. I tested it using NetBeans and it works alright.

 
Rob Spoor
Sheriff
Pie
Posts: 20495
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You know of course that it is bad practice to have code like ?

First of all, NEVER EVER just catch an exception and ignore it. The least you should do is print / log it - a simpel ex.printStackTrace() would be better than nothing at all.
Second, catching Exception or Throwable is also not recommended. You know which exceptions can be thrown (IOException for the reading, ClassCastException for the casting and ClassNotFoundException if the class read cannot be found), so catch them separately.
 
henri henri
Ranch Hand
Posts: 115
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok, Rob thanks for the update...
 
henri henri
Ranch Hand
Posts: 115
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
sonia pandit
Ranch Hand
Posts: 137
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can someone tell me if you try to delete a member from the hashmap and the key is not present in the hashmap, do you get a npe or it does nothing? Just want to be safe.

Thanks,

Veena
 
Rob Spoor
Sheriff
Pie
Posts: 20495
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Was it necessary to kick such an old thread when the answer is clearly mentioned in the java.util.Map and java.util.HashMap APIs?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic