java Arraylist 等于工作

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32501747/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 20:18:03  来源:igfitidea点击:

Arraylist Equals working

java

提问by Amit

Have one doubt regard equals method working in Array List, Below code snip it print true.

有一个疑问,关于在 Array List 中工作的 equals 方法,下面的代码将其截断打印为 true。

     ArrayList<String> s = new ArrayList<String>();

     ArrayList<Integer> s1 = new ArrayList<Integer>();

     System.out.println(s1.equals(s));

Is any one have idea why it's giving true ans.

有没有人知道为什么它给出真正的答案。

回答by Suresh Atta

Look the docfor the equals()method of ArrayList

查看文档equals()方法ArrayList

Returns true if and only if the specified object is also a list, both lists have the same size, and all corresponding pairs of elements in the two lists are equal.

当且仅当指定的对象也是一个列表,两个列表的大小相同,并且两个列表中所有对应的元素对都相等时,才返回 true。

Since there are no elements, all the conditions satisfied and hence true.

由于没有元素,所有条件都满足,因此true

If you add elements to the both list (atleast one in each), to see the desired output.

如果将元素添加到两个列表中(每个中至少有一个),以查看所需的输出。

回答by Adam Michalik

The contract of the List.equalsis that two lists are equal if all their elements are equal (in terms of equals()). Here, both are empty lists, so they are equal. The generic type is irrelevant, as there are anyway no list elements to compare.

的契约List.equals是如果两个列表的所有元素都相等(就 而言equals()),则它们相等。在这里,两者都是空列表,所以它们是相等的。泛型类型无关紧要,因为无论如何都没有要比较的列表元素。

However, they are not equal in terms of ==as these are two different objects.

然而,它们并不相等,==因为它们是两个不同的对象。

See this questionfor details between equals()and ==

这个问题的细节之间equals()==

回答by SomeJavaGuy

Here is the ArrayList implementation of the equals method from the AbstractList with a few commments what it actually does:

这是来自 AbstractList 的 equals 方法的 ArrayList 实现,并附有一些注释,它实际上是做什么的:

public boolean equals(Object o) {
   if (o == this) // Not the same list so no return
       return true;
   if (!(o instanceof List)) // is an instance of List, so no return 
       return false;

   ListIterator<E> e1 = listIterator();
   ListIterator<?> e2 = ((List<?>) o).listIterator();
   while (e1.hasNext() && e2.hasNext()) { // Both have no next, so no loop    here
        E o1 = e1.next();
        Object o2 = e2.next();
        if (!(o1==null ? o2==null : o1.equals(o2)))
            return false;
   }
   return !(e1.hasNext() || e2.hasNext()); // Both validate to false, so negating false return true in the end.
} 

回答by Farrandu

As previous answers pointed, equals returns true because both objects are instances of Listand have the same size (0).

正如前面的答案所指出的,equals 返回 true,因为两个对象都是 的实例List并且具有相同的大小 (0)。

It's also worth mentioning that the fact that one Listcontains Integerand the other Stringdoes not affect the behaviour because of type erasurein Java.

还值得一提的是,由于Java 中的类型擦除,一个List包含Integer另一个String不会影响行为。