比较两个对象,其中任何一个都可以为null [英] Comparing two objects, either of which could be null

查看:514
本文介绍了比较两个对象,其中任何一个都可以为null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的一个类中有一个函数,它将自己与同一个类的另一个实例进行比较 - 并找出哪些变量不同。这是为了最大限度地减少主数据库的网络负载(仅上传需要上传的数据,而不是上传整个对象)。

I have a function in one of my classes that compares itself with another instance of the same class - and finds out which variables differ. This is for the purpose of minimizing network load with a main database (by only uploading data that needs to be uploaded, instead of uploading the whole object).

为此,我一直在尝试使用 object.equals()函数来比较这两个对象。

For this, I have been trying to make use of the object.equals() function to compare the two objects.

I很快发现 object.equals()没有处理 null s,并在读完这个问题,我理解为什么。

I soon found that the object.equals() does not handle nulls, and after reading this question, I understand why.

所以我破坏的代码示例如下:

So an example of my broken code is as follows:

public class MyObject {

    String myString;
    String myString2;

    public String getChangedVars(MyObject comparisonObj) {
        ArrayList<String> changedVars = new ArrayList<String>();

        if (!this.myString.equals(comparisonObj.myString))
            changedVars.add("myString");
        if (!this.myString2.equals(comparisonObj.myString2))
            changedVars.add("myString2");

        return changedVars.toString();
    }
}

我的问题是 - 基于 要比较的其中一个变量可能为空,比较两个变量的简单方法是什么,同时避免 NullPointerException

My question is - on the basis that either one of the variables being compared could be null, what is a simple way to compare two variables whilst avoiding a NullPointerException?

编辑:
简单地检查两个对象上的null首先不能正常工作,因为我仍然想要比较对象有没有参考。例如,如果一个项目是 null 而另一个项目不是,我希望这个解析为 true ,作为变量已更改

Simply checking for null on both objects first doesn't work well, as I still want to compare if the object has a reference or not. Eg, if one item is null and the other is not, I want this to resolve to true, as the variable has changed.

推荐答案

自1.7以来jdk中有新的实用程序类 对象

There is new utility class available in jdk since 1.7 that is Objects .


此类包含用于对
对象进行操作的静态实用程序方法。这些实用程序包括null-safe或null-tolerant方法
,用于计算对象的哈希码,返回
对象的字符串,以及比较两个对象。

This class consists of static utility methods for operating on objects. These utilities include null-safe or null-tolerant methods for computing the hash code of an object, returning a string for an object, and comparing two objects.

你可以使用 Objects.equals ,它处理null。

You can use Objects.equals, it handles null.

Objects.equals(Object a,Object b)如果参数彼此相等则返回true,否则返回false
。因此,如果两个参数都为null,则返回true
,如果只有一个参数为null,则返回false。否则,
相等是通过使用第一个
参数的equals方法确定的。

Objects.equals(Object a, Object b) Returns true if the arguments are equal to each other and false otherwise. Consequently, if both arguments are null, true is returned and if exactly one argument is null, false is returned. Otherwise, equality is determined by using the equals method of the first argument.



if(Objects.equals(myString,myString2)){...}

这篇关于比较两个对象,其中任何一个都可以为null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆