• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Jeanne Boyarsky
  • Junilu Lacar
  • Henry Wong
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Frits Walraven
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • salvin francis
  • fred rosenberger

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

 
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
  • 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: 23877
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
Thanks Henry..
 
straws are for suckers. tiny ads are for attractive people.
Devious Experiments for a Truly Passive Greenhouse!
https://www.kickstarter.com/projects/paulwheaton/greenhouse-1
    Bookmark Topic Watch Topic
  • New Topic