JDK-6812862 : provide customizable hash() algorithm in HashMap for speed tuning
  • Type: Enhancement
  • Component: core-libs
  • Sub-Component: java.util:collections
  • Affected Version: 6
  • Priority: P5
  • Status: Closed
  • Resolution: Won't Fix
  • OS: windows_xp
  • CPU: x86
  • Submitted: 2009-03-04
  • Updated: 2021-03-03
  • Resolved: 2009-03-04
Description
A DESCRIPTION OF THE REQUEST :
HashMap always invokes the hashCode() method of the key object and after optimizes this hash by package private static int hash(int h).

Please provide following additional method for java.util.HashMap, which could be overridden if reasonable:

    protected int hash(K key) {
        return hash(key.hashCode());
    }

and replace all internal calls
        hash(key.hashCode())
by
        hash(key)


JUSTIFICATION :
See, for example, following String list as keys:
"windows-1250"
"windows-1251"
"windows-1252"
"windows-1253"
...
"IBM01140"
"IBM01141"
"IBM01142"
"IBM01143"
...

Calculation of hash by standard String#hashCode + optimization by static int hash(int h) will waste performance, as only taking the 2 last letters of those keys would suffice for a good fast hash value.

String#hashCode() method is not overrideable, because String is immutable.



---------- BEGIN SOURCE ----------
For example above, I would like to tune HashMap by following code:

    HashMap<String,String> x = new HashMap() {
        protected int hash(String key) {
            int len = key.length();
            return key.charAt(--len) ^ (key.charAt(--len) << 4);
        }
    };


---------- END SOURCE ----------

Comments
EVALUATION implement a wrapper class for the "key" to customize its hash() & equals() methods
04-03-2009