pri*_*ine 162 java null equality
我想在Java中比较两个字符串的相等性,当其中一个或两个都可以时null,所以我不能简单地调用.equals().什么是最好的方法?
boolean compare(String str1, String str2) {
...
}
Run Code Online (Sandbox Code Playgroud)
编辑:
return ((str1 == str2) || (str1 != null && str1.equals(str2)));
Run Code Online (Sandbox Code Playgroud)
Mar*_*eel 266
从Java 7开始,您可以使用静态方法java.util.Objects.equals(Object, Object)对两个对象执行equals检查,而无需关心它们null.
如果两个对象都null将返回true,如果一个是,null而另一个不是它将返回false.否则,它将返回equals使用第二个作为参数调用第一个对象的结果.
Eri*_*ric 161
这是Java内部代码使用的(在其他compare方法上):
public static boolean compare(String str1, String str2) {
return (str1 == null ? str2 == null : str1.equals(str2));
}
Run Code Online (Sandbox Code Playgroud)
Lui*_*oza 42
对于这些情况,最好使用Apache Commons StringUtils #equals,它已经处理了空字符串.代码示例:
public boolean compare(String s1, String s2) {
return StringUtils.equals(s1, s2);
}
Run Code Online (Sandbox Code Playgroud)
如果你不希望添加库,刚才复制的源代码中的StringUtils#equals方法,当你需要它运用它.
Nam*_*ace 27
对于那些不能使用API 19的Objects.equals(str1,str2)的android,有:
android.text.TextUtils.equals(str1, str2);
Run Code Online (Sandbox Code Playgroud)
它是安全的.它很少使用更昂贵的string.equals()方法,因为Android上的相同字符串几乎总是与"=="操作数比较,这要归功于Android的字符串池,并且长度检查是过滤掉大多数不匹配的快速方法.
源代码:
/**
* 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;
}
Run Code Online (Sandbox Code Playgroud)
从版本3.5开始,Apache Commons StringUtils具有以下方法:
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)
Run Code Online (Sandbox Code Playgroud)
这些提供null安全字符串比较.
使用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);
}
Run Code Online (Sandbox Code Playgroud)
使用 Apache Commons StringUtils 类的 equals(-,-) 和 equalsIgnoreCase(-,-) 方法比较两个字符串。
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
Run Code Online (Sandbox Code Playgroud)
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
Run Code Online (Sandbox Code Playgroud)