关于java数组的hashCode(),equals(),toString()的基本类型的特殊处理
##数组的基本类型的hashcode值的获取
public static int hashCode(Object obj) {
if (obj == null) {
return 0;
}
final Class<?> clazz = obj.getClass();
if (!clazz.isArray()) {//非数组直接返回hashcode
return obj.hashCode();
}
//获取数组的类型
final Class<?> componentType = clazz.getComponentType();
if (long.class.equals(componentType)) {
return Arrays.hashCode((long[]) obj);
} else if (int.class.equals(componentType)) {
return Arrays.hashCode((int[]) obj);
} else if (short.class.equals(componentType)) {
return Arrays.hashCode((short[]) obj);
} else if (char.class.equals(componentType)) {
return Arrays.hashCode((char[]) obj);
} else if (byte.class.equals(componentType)) {
return Arrays.hashCode((byte[]) obj);
} else if (boolean.class.equals(componentType)) {
return Arrays.hashCode((boolean[]) obj);
} else if (float.class.equals(componentType)) {
return Arrays.hashCode((float[]) obj);
} else if (double.class.equals(componentType)) {
return Arrays.hashCode((double[]) obj);
} else {
return Arrays.hashCode((Object[]) obj);
}
}
##数组的基本类型的equals()处理
public static boolean equals(Object thisObj, Object thatObj) {
if (thisObj == null) {
return thatObj == null;
} else if (thatObj == null) {
return false;
}
final Class<?> clazz = thisObj.getClass();
if (!clazz.equals(thatObj.getClass())) {
return false;
}
if (!clazz.isArray()) {
return thisObj.equals(thatObj);
}
final Class<?> componentType = clazz.getComponentType();
if (long.class.equals(componentType)) {
return Arrays.equals((long[]) thisObj, (long[]) thatObj);
} else if (int.class.equals(componentType)) {
return Arrays.equals((int[]) thisObj, (int[]) thatObj);
} else if (short.class.equals(componentType)) {
return Arrays.equals((short[]) thisObj, (short[]) thatObj);
} else if (char.class.equals(componentType)) {
return Arrays.equals((char[]) thisObj, (char[]) thatObj);
} else if (byte.class.equals(componentType)) {
return Arrays.equals((byte[]) thisObj, (byte[]) thatObj);
} else if (boolean.class.equals(componentType)) {
return Arrays.equals((boolean[]) thisObj, (boolean[]) thatObj);
} else if (float.class.equals(componentType)) {
return Arrays.equals((float[]) thisObj, (float[]) thatObj);
} else if (double.class.equals(componentType)) {
return Arrays.equals((double[]) thisObj, (double[]) thatObj);
} else {
return Arrays.equals((Object[]) thisObj, (Object[]) thatObj);
}
}
##数组的基本类型的toString()处理
public static String toString(Object obj) {
if (obj == null) {
return "null";
}
final Class<?> clazz = obj.getClass();
if (!clazz.isArray()) {
return obj.toString();
}
final Class<?> componentType = obj.getClass().getComponentType();
if (long.class.equals(componentType)) {
return Arrays.toString((long[]) obj);
} else if (int.class.equals(componentType)) {
return Arrays.toString((int[]) obj);
} else if (short.class.equals(componentType)) {
return Arrays.toString((short[]) obj);
} else if (char.class.equals(componentType)) {
return Arrays.toString((char[]) obj);
} else if (byte.class.equals(componentType)) {
return Arrays.toString((byte[]) obj);
} else if (boolean.class.equals(componentType)) {
return Arrays.toString((boolean[]) obj);
} else if (float.class.equals(componentType)) {
return Arrays.toString((float[]) obj);
} else if (double.class.equals(componentType)) {
return Arrays.toString((double[]) obj);
} else {
return Arrays.toString((Object[]) obj);
}
}
数组基本类的这个三个方法需要借助工具方法才能够实现,原因是当不用工具类Arrays的方法不会造成数组的值位置发生变化时,结果却没有改变,因为Arrays类用了不同的算法,以保证位置改变的时候,数组值运算的相应hash值发生变化。