Click here to Skip to main content
15,867,834 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
import java.util.ArrayList;

class HashingTest
{
	public static void main(String a[])
	{
		ArrayList ar=new ArrayList();
		ArrayList ar2=new ArrayList();
		
		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)+ " & hashcode          
                is"+ar.hashCode());
		System.out.print("Value of ar2[0] is "+ar2.get(0)+ " & 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 :-( ...
Can Anyone put some light on this ?
Posted

1 solution

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.
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900