方法描述说:
如果参数彼此非常相等,则返回true,否则返回false .等式是通过使用第一个参数的等号方法来确定的。
这(对我来说)意味着,如果对象维护引用的每个对象也都是相等的,则对象是非常相等的。它们所引用的每一个对象也是相等的。还有..。
所以..。equality is determined by using the equals method of the first argument.
这与.equals()有何不同?假设我们在哪里适当地描述等于,对象等于另一个对象,那么对象的每个字段都等于它。
你能提供一个例子说明Objects.deepEquals()和Objects.equals()的区别吗?
发布于 2012-07-22 00:41:40
如果deepEquals方法的至少一个参数不是数组,那么Objects.deepEquals和Objects.equals是相同的。
发布于 2014-10-24 21:24:49
String[] firstArray = {"a", "b", "c"};
String[] secondArray = {"a", "b", "c"};
System.out.println("Are they equal 1 ? " + firstArray.equals(secondArray) );
System.out.println("Are they equal 2 ? " + Objects.equals(firstArray, secondArray) );
System.out.println("Are they deepEqual 1? " + Arrays.deepEquals(firstArray, secondArray) );
System.out.println("Are they deepEqual 2? " + Objects.deepEquals(firstArray, secondArray) );会回来
Are they equal 1 ? false
Are they equal 2 ? false
Are they deepEqual 1? true
Are they deepEqual 2? true为什么“浅”equals方法返回false?这是因为in Java, for arrays, equality is determined by object identity。在本例中,firstArray和secondArray是不同的对象。
因此,执行String[] secondArray = firstArray将返回所有四个测试的true。
发布于 2021-03-28 07:03:13
示例:
import java.util.Arrays;
import java.util.Objects;
public class Main {
public static void main(String[] args) {
Integer[] x = { 1, 2 };
Integer[] y = { 1, 2 };
System.out.println(Objects.equals(x, y)); // false
System.out.println(Objects.deepEquals(x, y)); // true
System.out.println(Arrays.equals(x, y)); // true
System.out.println(Arrays.deepEquals(x, y)); // true
System.out.println();
int[][] a = { { 1, 2 }, { 3, 4 } };
int[][] b = { { 1, 2 }, { 3, 4 } };
System.out.println(Objects.equals(a, b)); // false
System.out.println(Objects.deepEquals(a, b)); // true
System.out.println(Arrays.equals(a, b)); // false
System.out.println(Arrays.deepEquals(a, b)); // true
}
}文件和反编译代码:
如果参数彼此相等,Objects#equals(Object a, Object b)返回true,否则返回false。因此,如果两个参数都为null,则返回true,如果正好有一个参数为null,则返回false。否则,通过使用第一个参数的相等方法来确定相等。
public static boolean equals(Object a, Object b) {
return (a == b) || (a != null && a.equals(b));
}如果参数彼此之间深度相等,则Objects#deepEquals(Object a, Object b)返回true,否则返回false。两个空值是非常相等的。如果这两个参数都是数组,则使用Arrays.deepEquals中的算法来确定相等性。否则,通过使用第一个参数的相等方法来确定相等。
public static boolean deepEquals(Object a, Object b) {
if (a == b)
return true;
else if (a == null || b == null)
return false;
else
return Arrays.deepEquals0(a, b);
}如果两个指定的对象数组相等,则Arrays#equals(Object[] a, Object[] a2)返回true。如果两个数组都包含相同数量的元素,并且两个数组中所有对应的元素对是相等的,则这两个数组被认为是相等的。
public static boolean equals(Object[] a, Object[] a2) {
if (a==a2)
return true;
if (a==null || a2==null)
return false;
int length = a.length;
if (a2.length != length)
return false;
for (int i=0; i<length; i++) {
if (!Objects.equals(a[i], a2[i]))
return false;
}
return true;
}如果两个指定的数组深度相等,则Arrays#deepEquals(Object[] a1, Object[] a2)返回true。与equals(Object[],Object[])方法不同,此方法适用于任意深度的嵌套数组。
public static boolean deepEquals(Object[] a1, Object[] a2) {
if (a1 == a2)
return true;
if (a1 == null || a2==null)
return false;
int length = a1.length;
if (a2.length != length)
return false;
for (int i = 0; i < length; i++) {
Object e1 = a1[i];
Object e2 = a2[i];
if (e1 == e2)
continue;
if (e1 == null)
return false;
// Figure out whether the two elements are equal
boolean eq = deepEquals0(e1, e2);
if (!eq)
return false;
}
return true;
}
static boolean deepEquals0(Object e1, Object e2) {
assert e1 != null;
boolean eq;
if (e1 instanceof Object[] && e2 instanceof Object[])
eq = deepEquals ((Object[]) e1, (Object[]) e2);
else if (e1 instanceof byte[] && e2 instanceof byte[])
eq = equals((byte[]) e1, (byte[]) e2);
else if (e1 instanceof short[] && e2 instanceof short[])
eq = equals((short[]) e1, (short[]) e2);
else if (e1 instanceof int[] && e2 instanceof int[])
eq = equals((int[]) e1, (int[]) e2);
else if (e1 instanceof long[] && e2 instanceof long[])
eq = equals((long[]) e1, (long[]) e2);
else if (e1 instanceof char[] && e2 instanceof char[])
eq = equals((char[]) e1, (char[]) e2);
else if (e1 instanceof float[] && e2 instanceof float[])
eq = equals((float[]) e1, (float[]) e2);
else if (e1 instanceof double[] && e2 instanceof double[])
eq = equals((double[]) e1, (double[]) e2);
else if (e1 instanceof boolean[] && e2 instanceof boolean[])
eq = equals((boolean[]) e1, (boolean[]) e2);
else
eq = e1.equals(e2);
return eq;
}https://stackoverflow.com/questions/11596884
复制相似问题