• 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
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

A general question regarding hashcode from K&B(scjp5)

 
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A general question regarding hashcode from K&B(scjp5) page 533.now we know that if the hashcode value is defined like the
one in class car regardless of the whether the equals method return true or false;all the objects land in the same bucket
time taking to locate the correct object;
Now my question is if we delete a car key from a hashmap,will all the map entries for all keys of type car be deleted??
I am not clear on this.can someone explain please?
public class Car {
private model;
public Car(String model) {
this.model=model;
}
public int hashcode() {
return 1492;
}
}
 
author
Posts: 23919
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

mohammad shaid wrote:
Now my question is if we delete a car key from a hashmap,will all the map entries for all keys of type car be deleted??



From the Javadoc, Map interface, remove() method...

remove

public Object remove(Object key)

Removes the mapping for this key from this map if it is present (optional operation). More formally, if this map contains a mapping from key k to value v such that (key==null ? k==null : key.equals(k)), that mapping is removed. (The map can contain at most one such mapping.)



So... only the object who are considered equal by the equals() method will get deleted.

Henry
 
mohammad shaid
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Henry..
 
Is that a spider in your hair? Here, threaten it with this tiny ad:
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic