java手动计算HashCode

 public static void countHash(Object object ) throws NoSuchFieldException, IllegalAccessException {
        Field field= Unsafe.class.getDeclaredField("theUnsafe");
        field.setAccessible(true);
        Unsafe unsafe=(Unsafe)field.get(null);
        long hashCode=0;
        for (long index = 7; index >0 ; index--) {
            //取Mark Word中的而每一个Byte进行计算
            hashCode |=(unsafe.getByte(object,index) & 0xFF) << ((index - 1) * 8 );
        }
        String code=Long.toHexString(hashCode);
        System.out.println("hashCode:0x"+code);
    }

猜你喜欢

转载自blog.csdn.net/qq_39648029/article/details/104263955