wood burning stoves 2.0*
The moose likes I/O and Streams and the fly likes Deleting HashMap Key Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » I/O and Streams
Bookmark "Deleting HashMap Key" Watch "Deleting HashMap Key" New topic
Author

Deleting HashMap Key

henri henri
Ranch Hand

Joined: Oct 03, 2005
Posts: 115
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

Joined: Jan 29, 2003
Posts: 8791

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 ]

A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19682
    
  20

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 ]

SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
henri henri
Ranch Hand

Joined: Oct 03, 2005
Posts: 115
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

Joined: Oct 27, 2005
Posts: 19682
    
  20

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

Joined: Oct 03, 2005
Posts: 115
ok, Rob thanks for the update...
henri henri
Ranch Hand

Joined: Oct 03, 2005
Posts: 115
sonia pandit
Ranch Hand

Joined: Apr 19, 2008
Posts: 137
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

Joined: Oct 27, 2005
Posts: 19682
    
  20

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?
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Deleting HashMap Key