Compare two objects in Java with possible null values

前端 未结 12 1478
忘掉有多难
忘掉有多难 2020-11-27 11:58

I want to compare two strings for equality when either or both can be null.

So, I can\'t simply call .equals() as it can contain null<

相关标签:
12条回答
  • 2020-11-27 12:22

    For these cases it would be better to use Apache Commons StringUtils#equals, it already handles null strings. Code sample:

    public boolean compare(String s1, String s2) {
        return StringUtils.equals(s1, s2);
    }
    

    If you dont want to add the library, just copy the source code of the StringUtils#equals method and apply it when you need it.

    0 讨论(0)
  • 2020-11-27 12:22
    boolean compare(String str1, String str2) {
        if (str1 == null || str2 == null)
            return str1 == str2;
    
        return str1.equals(str2);
    }
    
    0 讨论(0)
  • 2020-11-27 12:26
    boolean compare(String str1, String str2) {
      return (str1==null || str2==null) ? str1 == str2 : str1.equals(str2);
    }
    
    0 讨论(0)
  • 2020-11-27 12:28

    Since version 3.5 Apache Commons StringUtils has the following methods:

    static int  compare(String str1, String str2)
    static int  compare(String str1, String str2, boolean nullIsLess)
    static int  compareIgnoreCase(String str1, String str2)
    static int  compareIgnoreCase(String str1, String str2, boolean nullIsLess)
    

    These provide null safe String comparison.

    0 讨论(0)
  • 2020-11-27 12:32

    For those on android, who can't use API 19's Objects.equals(str1, str2), there is this:

    android.text.TextUtils.equals(str1, str2);
    

    It is null safe. It rarely has to use the more expensive string.equals() method because identical strings on android almost always compare true with the "==" operand thanks to Android's String Pooling, and length checks are a fast way to filter out most mismatches.

    Source Code:

    /**
     * Returns true if a and b are equal, including if they are both null.
     * <p><i>Note: In platform versions 1.1 and earlier, this method only worked  well if
     * both the arguments were instances of String.</i></p>
     * @param a first CharSequence to check
     * @param b second CharSequence to check
     * @return true if a and b are equal
     */
    public static boolean equals(CharSequence a, CharSequence b) {
        if (a == b) return true;
        int length;
        if (a != null && b != null && (length = a.length()) == b.length()) {
            if (a instanceof String && b instanceof String) {
                return a.equals(b);
            } else {
                for (int i = 0; i < length; i++) {
                    if (a.charAt(i) != b.charAt(i)) return false;
                }
                return true;
            }
        }
        return false;
    }
    
    0 讨论(0)
  • 2020-11-27 12:33

    You can use java.util.Objects as following.

    public static boolean compare(String str1, String str2) {
        return Objects.equals(str1, str2);
    }
    
    0 讨论(0)
提交回复
热议问题