Click here to Skip to main content
13,046,464 members (72,641 online)
Rate this:
Please Sign up or sign in to vote.
See more:
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 :-( ...
Can Anyone put some light on this ?
Posted 29-Sep-11 20: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
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web02 | 2.8.170713.1 | Last Updated 30 Sep 2011
Copyright © CodeProject, 1999-2017
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