This week's book giveaway is in the OCAJP forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide 1Z0-808 and have Jeanne Boyarsky & Scott Selikoff on-line! See this thread for details.
1. Is it possible for two objects with the same hashcode to act as keys in a hashtable? If yes, then please give me some examples.
2. I know If I want to sort objects (like employee objects) in a HashMap, I need to make my Object ((like employee objects)) which acts as the key in the HashMap entry to implement the Comparator interface and then implement the compare method which will sort on say employee salary. Can you please tell me how can I implement the equals method at the same time while sorting objects?
1. Yes. Keys are compared by the equals() method. If they have the same hashCode, it is just not as efficient. The following is a perfeclty legal, but very poor quality code, since the hashCode is only depening on the length of one String - name.
2. In order to sort, you need to implement the compareTo method, which doesn't have to be related to the equals() method at all.
[ November 15, 2006: Message edited by: �dne Brunborg ]