Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: JDK
import java.util.ArrayList;
 
class HashingTest
{
    public static void main(String a[])
    {
        ArrayList ar=new ArrayList();
        ArrayList ar2=new ArrayList();
 
<pre>
    ar.add(4);
    ar.add(5);
    ar.add(6);
 
    ar2.add(4);
    ar2.add(5);
    ar2.add(6);
 
    System.out.println("Value of ar[0] is "+ar.get(0)+ " &amp; hashcode
 
            is"+ar.hashCode());
    System.out.print("Value of ar2[0] is "+ar2.get(0)+ " &amp; hashcode
 
            is"+ar2.hashCode());
 
}

}
Output :-
Value of ar[0] is 4 & hashcode is33796
Value of ar2[0] is 4 & hashcode is33796

In the above program, i have created 2 instances of class ArrayList as "ar & ar2".
By hashing theory, no two different instances have similar hashcode. but when i run this program i got same output for both instances .Got confused Frown | :-( ...
Can Anyone put some light on this ?
Posted 29-Sep-11 21:28pm

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Hash computations operate on the objects data contents so it will compute the hash for the value "4" every time as both are the same integers.
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 490
1 Sergey Alexandrovich Kryukov 405
2 Maciej Los 285
3 ProgramFOX 265
4 Peter Leow 210
0 OriginalGriff 490
1 Sergey Alexandrovich Kryukov 405
2 Maciej Los 285
3 ProgramFOX 265
4 Peter Leow 210


Advertise | Privacy | Mobile
Web03 | 2.8.150331.1 | Last Updated 30 Sep 2011
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100