In Java you can override the equals and hash method of the HashMap's key object to determine how the hash code is generated and when two key objects should be considered equal.
Is there any Map implementation that allows to define the hash and equals on in the Map class by overriding its hash method (and determining key equality by an equals(key1,key2) method that can be overridden)?
Use case:
Ĺets assume we have a Objects of class GeoData, containing the fields: country, region, city. We want access two Maps: map x stores the number of inhabitants of each region and map y the number of inhabitants of each city.
To get both informations for a GeoData object we first need to extract country and region from that object, then create a new Object of class X that defines hash and equals considering country and region and use it as key for map x. Additionally we need to do the same with country, region and city. Create a new Object of class Y and use that get the value from map y.
Wouldn't it be easier if we extend a Map implementation for each of these maps x and y and override a hash(GeoData key) and equals(GeoData key1,GeoData key2) method, which would also avoid to create new key objects for every access?
hash(GeoData key) could then either use country and region in map x, or country,region and city in map y for hash code calculation.
UPDATE:
Correctly marked as duplicate. This answer suggesting apache commons-collections AbstractHashMap is what I was looking for.