Java方法哈希码的实现 [英] Implementation of method hashcode in java

查看:59
本文介绍了Java方法哈希码的实现的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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());
		
	
	}
	
}



输出:-
ar [0]的值为4&.哈希码为33796
ar2 [0]的值是4&.哈希码为33796



在上面的程序中,我创建了2个ArrayList类的实例,作为"ar& ar2". 根据哈希理论,没有两个不同的实例具有相似的哈希码.但是当我运行该程序时,两个实例的输出都相同.混淆了:-( ...
任何人都可以对此进行说明吗?



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 ?

推荐答案

哈希计算对对象数据内容进行操作,因此每次都将为值"4"计算哈希相同的整数.
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.


这篇关于Java方法哈希码的实现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆