Compare two objects in Java with possible null values

前端 未结 12 1479
忘掉有多难
忘掉有多难 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:35

    This is what Java internal code uses (on other compare methods):

    public static boolean compare(String str1, String str2) {
        return (str1 == null ? str2 == null : str1.equals(str2));
    }
    
    0 讨论(0)
  • 2020-11-27 12:39
    boolean compare(String str1, String str2) {
        if(str1==null || str2==null) {
            //return false; if you assume null not equal to null
            return str1==str2;
        }
        return str1.equals(str2);
    }
    

    is this what you desired?

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

    Since Java 7 you can use the static method java.util.Objects.equals(Object, Object) to perform equals checks on two objects without caring about them being null.

    If both objects are null it will return true, if one is null and another isn't it will return false. Otherwise it will return the result of calling equals on the first object with the second as argument.

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

    Using Java 8:

    private static Comparator<String> nullSafeStringComparator = Comparator
            .nullsFirst(String::compareToIgnoreCase); 
    
    private static Comparator<Metadata> metadataComparator = Comparator
            .comparing(Metadata::getName, nullSafeStringComparator)
            .thenComparing(Metadata::getValue, nullSafeStringComparator);
    
    public int compareTo(Metadata that) {
        return metadataComparator.compare(this, that);
    }
    
    0 讨论(0)
  • 2020-11-27 12:45

    Compare two string using equals(-,-) and equalsIgnoreCase(-,-) method of Apache Commons StringUtils class.

    StringUtils.equals(-, -) :

    StringUtils.equals(null, null)   = true
    StringUtils.equals(null, "abc")  = false
    StringUtils.equals("abc", null)  = false
    StringUtils.equals("abc", "abc") = true
    StringUtils.equals("abc", "ABC") = false
    

    StringUtils.equalsIgnoreCase(-, -) :

    StringUtils.equalsIgnoreCase(null, null)   = true
    StringUtils.equalsIgnoreCase(null, "abc")  = false
    StringUtils.equalsIgnoreCase("xyz", null)  = false
    StringUtils.equalsIgnoreCase("xyz", "xyz") = true
    StringUtils.equalsIgnoreCase("xyz", "XYZ") = true
    
    0 讨论(0)
  • 2020-11-27 12:46

    OK, so what does "best possible solution" mean?

    If you mean most readable, then all the possible solutions are pretty much equivalent for an experienced Java programmer. But IMO the most readable is this

     public boolean compareStringsOrNulls(String str1, String str2) {
         // Implement it how you like
     }
    

    In other words, hide the implementation inside a simple method that (ideally) can be inlined.

    (You could also "out-source" to a 3rd party utility library ... if you already use it in your codebase.)


    If you mean most performant, then:

    1. the most performant solution depends on the platform and the context,
    2. one of the "context" issues is the relative (dynamic) frequency of occurrence of null arguments,
    3. it probably doesn't matter which version is faster ... because the difference is probably too small to make a difference to the overall application performance, and
    4. if it does matter, the only way to figure out which is fastest ON YOUR PLATFORM is to try both versions and measure the difference.
    0 讨论(0)
提交回复
热议问题