JDK-1258091 : String.hashCode() produces the same value for too many unequal strings.
  • Type: Bug
  • Component: core-libs
  • Sub-Component: java.util
  • Affected Version: 1.0.2
  • Priority: P3
  • Status: Closed
  • Resolution: Fixed
  • OS: solaris_2.4
  • CPU: sparc
  • Submitted: 1996-07-03
  • Updated: 1999-01-19
  • Resolved: 1999-01-19
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availability Release.

To download the current JDK release, click here.
Other
1.2.0 1.2alphaFixed
Description
String.hashCode() produces identical hashCode values too often for strings that
are greater than 16 characters.  This is a significant problem because many other 
classes use String.hashCode() to generate a hashCode value

In the example below, the two strings are significantly different (different lengths, and
2/3 of the characters are different) yet produce the same hashcode.  

Steps to reproduce
Compile and run the attached code.

public class StringHash {

    public static void main(String argv[]) {
        String s1;
        String s2;

        if( argv.length >=2 ) {
            s1 = argv[0];
            s2 = argv[1];
        }
        else {
            s1 = "abcdefghijklmnopqrstuvwxyz";
            s2 = "a12d34g56j78m9!p@#s$%v^&y*A";
        }

        // compare hash codes
        System.out.println( "String 1: " + s1 + " s1.hashCode(): " + s1.hashCode() );
        System.out.println( "String 2: " + s2 + " s2.hashCode(): " + s2.hashCode() );
        System.out.println( "Equals: " + ( s1.equals( s2 ) ) + 
                            " HashCode equals: " + ( s1.hashCode() == s2.hashCode() ) );
        
    }

}

Comments
CONVERTED DATA BugTraq+ Release Management Values COMMIT TO FIX: generic FIXED IN: 1.2alpha INTEGRATED IN: 1.2alpha VERIFIED IN: 1.2fcs
14-06-2004

EVALUATION New Java string hash function in JDK1.2 takes care of this. See bug 4045622 for details. joshua.bloch@Eng 1997-07-25
25-07-1997