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

 

最新回复(0)
/jishuoRlpBiGcKFsv886ybl3ljzEZee5q3vKYa8bEyA_3D_3D4775715
8 简首页