• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Deleting HashMap Key

 
Ranch Hand
Posts: 115
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.

 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ok, Rob thanks for the update...
 
henri henri
Ranch Hand
Posts: 115
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
 
Ranch Hand
Posts: 137
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic