Given the following code.
class Book { String isbn; Book(String isbn) {this.isbn = isbn;} public int hashCode() { return 1234; } }
Select the correct option.
hashCode()
is inefficient.b
Method hashCode()
returns the same hash code for all the objects of this class.
This essentially makes all the values be stored in the same bucket if objects of the preceding classes are used as keys in class HashMap (or similar classes that use hashing), and reduces it to a linked list, drastically reducing its efficiency.
Option (a) in incorrect.
Book instances can be used to retrieve corresponding key values but only in limited cases-when you use the same keys (instances) to store and retrieve values.
Even though hashCode()
will return the same value for different Book instances, equals()
will always compare the reference variables and not their values, returning false.