@Override public int hashCode() { int result = 17; result = 31 * result + characterwiseCaseNormalize(getFirstName()).hashCode(); result = 31 * result + characterwiseCaseNormalize(getLastName()).hashCode(); return result; } private static String characterwiseCaseNormalize(String s) { StringBuilder sb = new StringBuilder(s); for(int i = 0; i < sb.length(); i++) { sb.setCharAt(i,Character.toLowerCase(Character.toUpperCase(sb.charAt(i)))); } return sb.toString(); }
This hashCode will match the equals defined by equalsIgnoreCase . Basically, according to the equalsIgnoreCase contract, this seems to rely on the fact that
Character.toLowerCase(Character.toUpperCase(c1))==Character.toLowerCase(Character.toUpperCase(c2))
whenever
Character.toLowerCase(c1)==Character.toLowerCase(c2).
I have no evidence that this is true, but the OpenJDK implementation of equalsIgnoreCase actually does this sequentially with this method; it checks if the corresponding characters are equal, that is, their versions in upper case are equal, that is, the versions in lower case of upper case versions are equal.
Robert Tupelo-Schneck
source share