小知识,大挑战!本文正在参与“程序员必备小知识”创作活动
1. 判断相等
1.1 Objects.equals()
java.util包中的Objects工具类的静态方法,接收两个参数,比较两个对象是否相等
//方法定义
public static boolean equals(Object a, Object b) {
return (a == b) || (a != null && a.equals(b));
}
//使用方法
String str1 = "123";
String str2 = "123 ";
Objects.equals(str1,str2); //false
1.2 o.equals()
java.lang包中的方法,对象引用,接收一个参数,用于判断两个对象或字符串类型是否相等。要保证使用该方法的对象不是null,否则会报异常。
//Object的equals方法
public boolean equals(Object obj) {
return (this == obj);
}
//String的equals方法
public boolean equals(Object anObject) {
if (this == anObject) {
return true;
}
if (anObject instanceof String) {
String anotherString = (String)anObject;
int n = value.length;
if (n == anotherString.value.length) {
char v1[] = value;
char v2[] = anotherString.value;
int i = 0;
while (n-- != 0) {
if (v1[i] != v2[i])
return false;
i++;
}
return true;
}
}
return false;
}
- 如果是对象的equals方法,会直接比较两个对象是否相等(==);
- 如果是字符串的equals方法,会先比较两个对象是否相等,如果不相等,会对字符串的每个字符进行比较,完全一致时也认为相等。
2. 字符串判空
2.1 str.isEmpty()
java.lang包中String类对象的方法,该方法用来判断字符串的长度是否为0。
注意:因为该方法是对象的方法,因此字符串为null时无法使用此方法,会报空指针异常。
public boolean isEmpty() {
return value.length == 0;
}
2.2 StringUtils.isEmpty()
位于Spring中的工具类包org.springframework.util中,字符串工具类的方法,传入字符串参数判断字符串是否为空。
//该方法在Spring5.3之后被弃用,@Deprecated注解表示弃用
@Deprecated
public static boolean isEmpty(@Nullable Object str) {
return str == null || "".equals(str);
}
//StringUtils.isEmpty()被弃用后,可以使用hasLength()或hasText来替代它
//hasLength(),判断字符串为null或为空字符串(长度为0)
public static boolean hasLength(@Nullable String str) {
return str != null && !str.isEmpty();
}
//hasText(),判断字符串是否为null、空字符串、空格字符串,containsText判断全是空格时
public static boolean hasText(@Nullable String str) {
return str != null && !str.isEmpty() && containsText(str);
}
2.3 apache.commons-lang3
apache commons是一个强大的工具类库,commons-lang3时java.lang的增强版。
使用时需要引入依赖
//依赖信息
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.12.0</version>
</dependency>
2.3.1StringUtils.isEmpty()
commons-lang3同样提供了isEmpty()方法,用来判断字符串是否为空,与java.lang中的该方法一样,实质上时判断字符串为null或者长度等于0.
public static boolean isEmpty(CharSequence cs) {
return cs == null || cs.length() == 0;
}
2.3.2 StringUtils.isBlank()
此中方法除了可以判断null和长度为0的字符串外,还会将全为空格字符的字符串判断为空值,与java.lang中的hasText()方法一致。
public static boolean isBlank(CharSequence cs) {
int strLen = length(cs);
if (strLen == 0) {
return true;
} else {
for(int i = 0; i < strLen; ++i) {
if (!Character.isWhitespace(cs.charAt(i))) {
return false;
}
}
return true;
}
}
//其中leng()方法会对null对象返回长度为0
public static int length(CharSequence cs) {
return cs == null ? 0 : cs.length();
}
//Character.isWhitespace()方法用来判断字符串的字符是否为空格,全为空格时返回true
3. 集合判空
3.1 CollectionUtils.isEmpty()
判断集合是null或者集合元素个数为0
public static boolean isEmpty(@Nullable Collection<?> collection) {
return collection == null || collection.isEmpty();
}
3.2 apache.commons-collections4
3.2.1 CollectionUtils.isEmpty()
apache.commons在判空上和Java自带逻辑一致
public static boolean isEmpty(final Collection<?> coll) {
return coll == null || coll.isEmpty();
}