在Java 7中的equals()和deepEquals()

问题描述:

方法介绍说:在Java 7中的equals()和deepEquals()

返回true如果参数是深深彼此相等,假 否则... 平等是通过使用equals方法确定第一个参数为 。

哪个(对我)建议对象是深深的平等,如果他们维护引用的每个对象也都等于使用equals()方法。而且他们提到的每一件物品也都是平等的。和..

所以.. equality is determined by using the equals method of the first argument.

是如何从.equals()这有什么不同?假设我们描述等于适当地,其中,对象是等于另一个目的是对象的每个字段等于它。

能否请您提供说明Objects.deepEquals()Objects.equals()之间的区别的例子吗?

如果至少有一个deepEquals方法的参数不是数组,那么Objects.deepEqualsObjects.equals是相同的。

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。在这个例子中,firstArraysecondArray是不同的对象。

String[] secondArray = firstArray因此而不是将返回true所有四个测试。

+0

伟大的答案,例如使得它很清楚。 – 2017-11-08 13:47:54

附加一个很好的实施例I上javarevisited.blogspot.in发现

public class ArrayCompareTest { 

public static void main(String args[]) { 

    //comparing primitive int arrays in Java 
    int[] i1 = new int[] {1,2,3,4}; 
    int[] i2 = new int[] {1,2,3,4}; 
    int[] i3 = new int[] {0,2,3,4}; 

    //Arrays.equals() compare Array and return true if both array are equal 
    //i..e either both of them are null or they are identical in length, and each pair 
    //match each other e.g. i[0]=i2[0], i[1]=i2[1] and so on 

    //i1 and i2 should be equal as both contains same elements 
    boolean result = Arrays.equals(i1, i2); 
    System.out.println("Comparing int array i1: " + Arrays.toString(i1) 
         + " and i1: " + Arrays.toString(i2)); 
    System.out.println("Does array i1 and i2 are equal : " + result); 

    //array ii2 and i3 are not equals as only length is same, first pair is not same 
    result = Arrays.equals(i2, i3); 
    System.out.println("Comparing int array i2: " + Arrays.toString(i2) 
         + " and i3: " + Arrays.toString(i3)); 
    System.out.println("Does array i2 and i3 are equal : " + result); 

    //comparing floating point or double arrays in Java 
    double[] d1 = new double[] {1.5, 2.4, 3.2, 4,1}; 
    double[] d2 = new double[] {1.5, 2.4, 3.2, 4,1}; 
    double[] d3 = new double[] {0.0, 2.4, 3.2, 4,1}; 

    //Comparing two floating-point arrays using Arrays.equals() in Java 

    //double array d1 and d2 should be equal - length same, each index matches 
    result = Arrays.equals(d1, d2); 
    System.out.println("Comparing double array d1: " + Arrays.toString(d1) 
         + " and d2: " + Arrays.toString(d2)); 
    System.out.println("Does double array d1 and d2 are equal : " + result); 

    //double array d2 and d3 is not equal - length same, first pair does not match 
    result = Arrays.equals(d2, d3); 
    System.out.println("Comparing double array d2: " + Arrays.toString(d2) 
         + " and d3: " + Arrays.toString(d3)); 
    System.out.println("Does double array d2 and d3 are same : " + result); 

    //comparing Object array, here we will use String array 
    String[] s1 = new String[]{"One", "Two", "Three"}; 
    String[] s2 = new String[]{"One", "Two", "Three"}; 
    String[] s3 = new String[]{"zero", "Two", "Three"}; 

    //String array s1 and s2 is equal - length same, each pair matches 
    result = Arrays.equals(s1, s2); 
    System.out.println("Comparing two String array s1: " + Arrays.toString(s1) 
         + " and s2: " + Arrays.toString(s2)); 

    System.out.println("Are both String array s1 and s2 are equal : " + result); 

    //String array s2 and s3 is not equal - length same, first pair different 
    result = Arrays.equals(d2, d3); 
    System.out.println("Comparing two String array s2: " + Arrays.toString(s2) 
         + " and s3: " + Arrays.toString(s3)); 

    System.out.println("Are both String array s2 and s3 are equal : " + result); 

    //Comparing nested arrays with equals and deepEquals method 
    //Arrays.equals() method does not compare recursively, 
    //while deepEquals() compare recursively 
    //if any element inside Array is type of Array itself, 
    //as here second element is String array 

    Object[] o1 = new Object[]{"one", new String[]{"two"}}; 
    Object[] o2 = new Object[]{"one", new String[]{"two"}}; 

    System.out.println("Object array o1: " + Arrays.toString(o1) + " and o2: " 
         + Arrays.toString(o2)); 
    System.out.println("Comparing Object Array o1 and o2 with Arrays.equals : " 
         + Arrays.equals(o1, o2)); 
    System.out.println("Comparing Object Array o1 and o2 with Arrays.deepEquals : " 
         + Arrays.deepEquals(o1, o2)); 
} 

}

输出: 比较int数组I1:[1,2,3,4]和I1:[ 1,2,3,4] 不阵列i1和i2是相等的:真

比较int数组12:[1,2,3,4]和i3:[0,2,3,4] 不阵列I2和I3是相等的:假

比较双阵列D1:[1.5,2.4,3.2,4.0,1.0]和d2:[1.5,2.4,3.2,4.0,1.0] 是否双阵列d1和d2是相等的:真

比较双阵列D2:[1.5,2.4,3.2,4.0,1.0]和D3:[0.0,2.4,3.2,4.0,1。0] 是否双阵列d2和d3是相同的:假

比较两个字符串数组S1:[一,二,三]和s2:[一,二,三] 都是字符串数组s1和s2是相等:真

比较两个字符串数组S2:[一,二,三]和s3:零,二,三] 都是字符串数组s2和s3是相等的:假

Object数组O1:一个,[Ljava.lang.String; @ 19821f]和o2:[one,[Ljava.lang.String; @ addbf1] 比较对象数组o1和o2与Arrays.equals:false 比较对象数组o1和o2与Arrays.deepEquals:true

deepEquals()与任意深度的嵌套数组一起使用。
equals()与简单的基本数据类型一起使用。
对于前:

public class TwoDArray { 
    public static void main(String args[]) { 
     int a[][] = new int[2][2]; 
     int b[][] = new int[2][2]; 
     for(int i=0;i<2;i++) 
      for(int j=0;j<2;j++) { 
       a[i][j] = i+j; 
       b[i][j] = i+j; 
      } 
     System.out.println(Arrays.deepEquals(a,b));//return true 
     System.out.println(Arrays.equals(a, b));//return false 
    } 
} 
+0

这不是一个好的答案,因为它不会解释问题的任何内容以及解决方法。请添加一些上下文信息,参考或解释。谢谢! – Clijsters 2017-09-01 09:45:42