Page 1 of 1
About Question enthuware.ocpjp.v7.2.1304 :
Posted: Mon Sep 01, 2014 6:45 am
by hamada.yamasaki
This will not help because it does not solve the basic problem of bad hashCode() method that exists with Book class. Moreover, if TreeMap is used, the second map.add() call will throw a ClassCastException because Book does not implement Comparable interface.
Even the first call to method map.put() will throw a ClassCastException. And more over ther is no method map.add() , i think you mean map.put() ?
Re: About Question enthuware.ocpjp.v7.2.1304 :
Posted: Mon Sep 01, 2014 8:40 am
by admin
You are right. Even the first call throws ClassCastException now. With previous versions of Java, the first call did not require any comparison and so there was no need to cast it to Comparable.
Re: About Question enthuware.ocpjp.v7.2.1304 :
Posted: Sat Jul 21, 2018 7:33 pm
by renzo2939
why
Book b = new Book("A111"); // in BookStore constructor
Book b = new Book("A111"); // in public static void main
has different hashCode?
Re: About Question enthuware.ocpjp.v7.2.1304 :
Posted: Sat Jul 21, 2018 9:59 pm
by admin
That's because the Book class doesn't implement the hashCode method. Therefore, the inherited version from the Object class is used. Object's hashCode returns a different value for a different object.