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

两段检验系统生成的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 =http://www.mamicode.com/ 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关系,

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