两段检验系统生成的identityHashCode是否重复的代码

前言:承接上一篇hashCode和identityHashCode 的关系,下面的两段简单的程序主要是检验一下系统生成的identityHashCode是否存在重复的情况。

1:可以自由控制生成对象的个数,并且不受测试的类是否重写hashCode()方法的影响

import java.util.HashSet;
import java.util.Set;

public class CheckSystemIdentity {
    public static void main(String args[]) {
        //声明set对象
        Set<Integer> hashSet = new HashSet<Integer>(1024);
        //通过循环遍历,检查生成的hashCode是否存在重复的现象
        int colls = 0;
        int cycleSize=1000000;
        for (int n = 0; n < cycleSize; n++) {
            Integer obj = new Integer(666);
            int identityHashCode = System.identityHashCode(obj);
            //System.out.println("identityHashCode is : "+identityHashCode);
            Integer hashValue = Integer.valueOf(identityHashCode);
            //System.out.println("hashValue is        : "+hashValue+"\n");
            if (hashSet.contains(hashValue)) {
                System.err.println("System.identityHashCode() collision!");
                colls++;
            }
            else {
                hashSet.add(hashValue);
            }
        }
        //System.out.println("Integer.MAX_VALUE is : "+Integer.MAX_VALUE+"\n");
        System.out.println("created "+cycleSize+" different objects - " + colls + " times with the same value for System.identityHashCode()");
    }

}

2:利用死循环来检测系统生成的identityHashCode是否存在重复的情况

2-1:测试类是自定义的,没有重写hashCode()方法

import java.util.Hashtable;
import java.util.Map;

public class HashCodeTest{
    //试验对象,没有重写hashCode()方法
    static class DummyObject{ }

    public static void reportClash(DummyObject obj1, DummyObject obj2) {
        System.out.println("obj1.hashCode() = " + obj1.hashCode());
        System.out.println("obj2.hashCode() = " + obj2.hashCode());
        System.out.println("(obj1 == obj2) = " + (obj1 == obj2) + " (!)");
    }

    public static void main(String[] args) {
        Map<Integer,DummyObject> map = new Hashtable<Integer ,DummyObject>();
        //通过死循环,检查生成的hashCode是否存在重复的情况
        for (int count = 1; true; count++) {
            DummyObject obj = new DummyObject();
            if (map.containsKey(obj.hashCode())) {
                System.out.println("Clash found after instantiating " + count + " objects.");
                reportClash(map.get(obj.hashCode()), obj);
                System.exit(0);
            }
            System.out.println("The method execute  " + count + " and the object hashCode is "+obj.hashCode());
            map.put(obj.hashCode(), obj);
        }
    }
}

2-2:测试类是String,重写了hashCode()方法和2-1正好再次的做一下对比

import java.util.Hashtable;
import java.util.Map;

public class HashCodeTest {

    public static void reportClash(String obj1, String obj2) {
        System.out.println("obj1.hashCode() = " + obj1.hashCode());
        System.out.println("obj2.hashCode() = " + obj2.hashCode());
        System.out.println("(obj1 == obj2) = " + (obj1 == obj2) + " (!)");
    }

    public static void main(String[] args) {
        Map<Integer,String> map = new Hashtable<Integer ,String>();
        //通过死循环,检查生成的hashCode是否存在重复的情况
        for (int count = 1; true; count++) {
            String obj = new String();
            if (map.containsKey(obj.hashCode())) {
                System.out.println("Clash found after instantiating " + count + " objects.");
                reportClash(map.get(obj.hashCode()), obj);
                System.exit(0);
            }
            System.out.println("The method execute  " + count + " and the object hashCode is "+obj.hashCode());
            map.put(obj.hashCode(), obj);
        }
    }
}

 3:程序相对简单,有兴趣的可以自己运行一下看看结果。

      结论如下:

      3-1:在我实验的环境中(win7+jdk7)identityHashCode没有出现重复的现象

      3-2:没有重写的hashCode和identityHashCode是一致的,可以间接的反映一个对象的内存地址是什么

      3-3:identityHashCode能更为准确的代表一个对象和其内存地址的hash关系,

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值