Java 比较两个对象并检查 null
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1402030/
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
Compare two objects with a check for null
提问by dcstraw
Is there a method in the JDK that compares two objects for equality, accounting for nulls? Something like this:
JDK 中是否有一种方法可以比较两个对象的相等性,并考虑空值?像这样的东西:
public static boolean equals(Object o1, Object o2)
{
if (o1 == null)
{
return o2 == null; // Two nulls are considered equal
}
else if (o2 == null)
{
return false;
}
return o1.equals(o2);
}
It seems silly to write this method myself since I would think that it has to exist already somewhere.
自己编写这个方法似乎很愚蠢,因为我认为它必须已经存在于某个地方。
采纳答案by icza
Java 7.0 added a new handy class: Objects
.
Java 7.0 添加了一个新的方便的类:Objects
.
It has a method exactly for this: Objects.equals(Object a, Object b)
它有一个专门用于此的方法: Objects.equals(Object a, Object b)
回答by matt b
If you are worried about NullPointerExceptions you could just test equality like:
如果您担心 NullPointerExceptions,您可以测试相等性,例如:
if (obj1 != null && obj1.equals(obj2)) { ... }
The general contract of equals()
is that a non-null object should never be equal to a null reference, and that the equals()
method should return false if you are comparing an object to a null reference (and not throw a NPE).
的一般约定equals()
是,非空对象永远不应等于空引用,并且equals()
如果将对象与空引用进行比较(而不是抛出 NPE),则该方法应返回 false。
回答by Yishai
Apache Commons Lang has such a method: ObjectUtils.equals(object1, object2). You don't want generics on such a method, it will lead to bogus compilation errors, at least in general use. Equals knows very well (or should - it is part of the contract) to check the class of the object and return false, so it doesn't need any additional type safety.
Apache Commons Lang 有这样一个方法:ObjectUtils.equals(object1, object2)。你不想在这样的方法上使用泛型,它会导致虚假的编译错误,至少在一般使用中是这样。Equals 非常清楚(或者应该 - 它是合同的一部分)检查对象的类并返回 false,因此它不需要任何额外的类型安全。
回答by SingleShot
Whenever I come across a need and think "this is so common Java must have it" but find it doesn't, I check the Jakarta Commons project. It almost always has it. A quick search of the commons-lang API (which has the most basic of common utilities) shows an equals()
method that provides what you want.
每当我遇到需要并认为“Java 必须拥有它”但发现它没有时,我会查看 Jakarta Commons 项目。它几乎总是有。快速搜索 commons-lang API(它具有最基本的通用实用程序)显示了equals()
一种提供您想要的方法。
回答by cjstehno
Jakarta Commons LangAPI has what you are looking for ObjectUtils.equals(Object,Object)
Jakarta Commons LangAPI 有你要找的东西ObjectUtils.equals(Object,Object)
回答by Edward Falk
FWIW, this was my implementation:
FWIW,这是我的实现:
private static boolean equals(Object a, Object b) {
return a == b || (a != null && a.equals(b));
}
In my application, I know that a and b will always be the same type, but I suspect this works fine even if they aren't, provided that a.equals() is reasonably implemented.
在我的应用程序中,我知道 a 和 b 将始终是相同的类型,但我怀疑即使它们不是,也可以正常工作,前提是 a.equals() 得到合理实现。
回答by farid_z
public static boolean equals(Object object1, Object object2) {
if (object1 == null || object2 == null) {
return object1 == object2;
}
return object1.equals(object2);
}