Click here to Skip to main content
11,487,406 members (71,365 online)
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 Sergey Alexandrovich Kryukov 9,063
1 OriginalGriff 8,287
2 Sascha Lefèvre 3,334
3 Maciej Los 2,946
4 Richard Deeming 2,370

Advertise | Privacy | Mobile
Web03 | 2.8.150520.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