United StatesChange Country, Oracle Worldwide Web Sites Communities I am a... I want to...
Bug ID: 1258091 String.hashCode() produces the same value for too many unequal strings.
1258091 : String.hashCode() produces the same value for too many unequal strings.

Details
Type:
Bug
Submit Date:
1996-07-03
Status:
Closed
Updated Date:
1999-01-19
Project Name:
JDK
Resolved Date:
1999-01-19
Component:
core-libs
OS:
solaris_2.4
Sub-Component:
java.util
CPU:
sparc
Priority:
P3
Resolution:
Fixed
Affected Versions:
1.0.2
Fixed Versions:
1.2.0

Related Reports

Sub Tasks

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


                                     
2004-06-14
EVALUATION

   New Java string hash function in JDK1.2 takes care of this.  See bug
4045622 for details.

joshua.bloch@Eng 1997-07-25
                                     
1997-07-25



Hardware and Software, Engineered to Work Together