Is there an implementation of the HashMap class (or map interface) that will allow me to use an alternative hash code and equal operations ... Just like collections of the same type can be sorted in several ways using Comparator in Collections.sort (list, comparator )
I would like, if possible, to avoid by creating a key wrapper that provides the required hash code and equal operation.
In my case, one of the scenarios is why I need something like this:
In my web application, for every request, I upload Location / ISP and other data. In different parts of the code (in the service and repository layers), I have "minimized" caches specific to its requirements.
Here is an example of simplified code:
class GeoIpData{
private String countryName;
private String state;
private String city;
private String isp;
@Override
public int hashCode() {
}
@Override
public boolean equals(Object obj) {
}
}
Map<GeoIpData,#Type1> fullCache = ...
Map<GeoIpData,#Type2> countryCache = ...
Map<GeoIpData,#Type2> ispCache = ...
, 3 - .
fullCache:
hashCode -> GeoIpData.hashCode();
equals -> GeoIpData.equals(Object obj);
countryCache:
hashCode -> {countryName hashCode }
equals -> {compare countryName }
ispCache:
hashCode -> {countryName hashCode & isp hashCode }
equals -> {compare countryName & compare isp hashCode }