Click here to Skip to main content
Rate this: bad
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();
		System.out.println("Value of ar[0] is "+ar.get(0)+ " & hashcode          
		System.out.print("Value of ar2[0] is "+ar2.get(0)+ " & 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
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.

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

  Print Answers RSS
0 Zoltán Zörgő 300
1 /\jmot 180
2 CHill60 145
3 Peter Leow 140
4 CPallini 135
0 Sergey Alexandrovich Kryukov 9,383
1 OriginalGriff 6,881
2 Peter Leow 4,607
3 Zoltán Zörgő 4,284
4 CHill60 2,892

Advertise | Privacy | Mobile
Web01 | 2.8.150129.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