OK. I understand how equals and hashcode work and how they are used in hashmap. But this question crossed my mind. What if I have a third-party object that has no overridden hashcode and is equal, and Iβm not even allowed to modify it.
Consider the following class:
//Unmodifiable class public final class WannaBeKey{ private String id; private String keyName; //Can be many more fields public String getId() { return id; } public String getKeyName() { return id; } //no hashcode or equals :( }
Now I want to make this class as my Hashmap key, it is obvious that it will not work without peers and hashcode. I want to know if there is a way to deal with such cases? I canβt think of anything, or I am above my head.
Thanks.
source share