我如何检查两个ArrayList的不同,我不在乎有什么变化 [英] How can I check if two ArrayList differ, I don't care what's changed
问题描述
我如何检查两个的ArrayList彼此有什么不同?我不在乎有什么区别,我只是想知道,如果他们不一样的。
How can I check if two ArrayLists differ from one another? I don't care what's the difference, I just want to know if they're not the same.
我从每分钟数据库中获取的分数列表,只有当我拿来分数列表是一个我以前取一分钟我想将它发送到客户端不同。
I'm fetching scores list from a database every minute, and only if the scores list that I fetched is different from the one I fetched a minute ago I want to send it to the client.
现在ArrayList的价值实际上是我创建了一个类(包含姓名,单板层积材,等级,分数)。
Now the value of the ArrayList is actually a class that I created (that contains name, lvl, rank, score).
我是否需要实施等于()
就可以了?
Do I need to implement equals()
on it?
推荐答案
正如勒夫指出,对于大多数应用,<一href=\"http://java.sun.com/javase/6/docs/api/java/util/List.html#equals%28java.lang.Object%29\"><$c$c>List.equals(Object O) 定义如下:
On the definition of "sameness"
As Joachim noted, for most application, List.equals(Object o)
definition works:
与此列表的相等比较指定对象。返回真正
当且仅当指定的对象也是一个列表,两个列表有相同的大小,并且两个列表的所有相应元素对都相等。 (两个元素 E1
和 E2
都是平等的,如果(E1 == NULL?E2 ==空:e1.equals(E2))
)换句话说,两个列表被定义为等于如果它们包含在相同的顺序相同的元件。此定义确保了等于
法正常工作跨越列表
接口的不同实现。
Compares the specified object with this list for equality. 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. (Two elementse1
ande2
are equal if(e1==null ? e2==null : e1.equals(e2))
.) In other words, two lists are defined to be equal if they contain the same elements in the same order. This definition ensures that theequals
method works properly across different implementations of theList
interface.
根据您如何使用它,虽然这可能无法正常工作。如果你有一个列表&LT; INT []&GT;
,例如,它因为数组继承完全不是那么回事等于
从对象
这平等定义为基准的身份。
Depending on how you're using it, though, this may not work as expected. If you have a List<int[]>
, for example, it doesn't quite work because arrays inherit equals
from Object
which defines equality as reference identity.
List<int[]> list1 = Arrays.asList(new int[] { 1, 2, 3 });
List<int[]> list2 = Arrays.asList(new int[] { 1, 2, 3 });
System.out.println(list1.equals(list2)); // prints "false"
此外,两个清单不同类型的参数可以是等于
:
List<Number> list1 = new ArrayList<Number>();
List<String> list2 = new ArrayList<String>();
System.out.println(list1.equals(list2)); // prints "true"
您还提到,列表必须包含具有相同类型的元件。下面是另一个例子,其中的元素没有相同的类型,但他们是等于
:
You also mentioned that the list must contain elements with the same type. Here's yet another example where the elements don't have the same type, and yet they're equals
:
List<Object> list1 = new ArrayList<Object>();
List<Object> list2 = new ArrayList<Object>();
list1.add(new ArrayList<Integer>());
list2.add(new LinkedList<String>());
System.out.println(list1.equals(list2)); // prints "true"
所以,除非你明确地定义什么平等对你意味着,这个问题可以有非常不同的答案。最实用的目的,不过, List.equals
就足够了。
更新后的信息显示, List.equals
将做的工作就好了,的提供的的元素实施等于
正常(因为列表&LT; E&GT; .equals
调用 E.equals
在非空
-elements,按照上面的API文档)。
Information after update suggests that List.equals
will do the job just fine, provided that the elements implement equals
properly (because List<E>.equals
invokes E.equals
on the non-null
-elements, per the API documentation above).
因此,在这种情况下,如果我们有,比如说,一个列表&LT;播放器&GT;
,那么播放器
必须 @覆盖的equals(对象O)
返回真正
如果 0的instanceof播放器
和相关领域,他们都等于
(对于引用类型)或 ==
(对于基元)。
So in this case, if we have, say, a List<Player>
, then Player
must @Override equals(Object o)
to return true
if o instanceof Player
and on the relevant fields, they're all equals
(for reference types) or ==
(for primitives).
当然,当你 @覆盖等于
,你也应该 @覆盖INT哈希code()
。勉强可接受的最低限度是 42的回报;
;稍微好一点的是收益name.hash code();
;最好是使用包括在其上定义的所有字段等于
的公式。一个好的IDE可以自动生成等于/哈希code
方法适合你。
Of course, when you @Override equals
, you should also @Override int hashCode()
. The barely acceptable minimum is to return 42;
; slightly better is to return name.hashCode();
; best is to use a formula that involves all the fields on which you define equals
. A good IDE can automatically generate equals/hashCode
methods for you.
- 的有效的Java第二版的
- 的第8项:等于重写时,服从总承包的
- 的项目9:始终重写hash code,当你重载equals 的
- Effective Java 2nd Edition
- Item 8: Obey the general contract when overriding equals
- Item 9: Always override hashcode when you override equals
- <$c$c>Object.equals(Object)$c$c>
- <$c$c>Object.hash$c$c()$c$c>
- <$c$c>java.lang.Comparable$c$c> - 这里没有必要的,但另一个重要的Java API合约
在
等于/哈希code
组合:- Overriding equals and hashcode in Java
- Why both hashCode() and equals() exist
- How to ensure hashCode() is consistent with equals()?
在
等于
VS==
:
equals和== 的 - 差异
- 为什么equals()方法时,我们有==操作符?
- 的Java String.equals与==
- Difference between equals and ==
- why equals() method when we have == operator?
- Java String.equals versus ==
这篇关于我如何检查两个ArrayList的不同,我不在乎有什么变化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!