1.1 字符串判空检查
要了解字符串判空方法的区别首先要理解对象为空字符串""
和null
的区别
1.1.1 ""
和 null
的区别
null
是没有地址的,可以理解为空指针。当对象在构造器初始化时,如果没有被显示的赋于初值,那么会默认赋值为null
。""
空字符串是一个String
对象是有地址的,只是内容是空。
关于构造器初始化,在没有显示赋予初值的情况下。默认将数值型赋为 0
, 布尔型是 false
,对象引用则是 null
。
String
并不是基本数据类型,而是对象所以会被默认的赋予 null
。
1.1.2 isEmpty()
与isBlank()
isEmpty()
和 isBlank()
区别在于isBlank()
可以多了对于空格的判断,可以根据方法名区别使用 isEmpty()
判断字符串是否为空,而isBlank()
则是判断字符串是否是空格,空或null
isEmpty(String str)
isEmpty()
,isNotEmpty()
判段字符串是否为空或者null
,空格返回false
/**
* <p>Checks if a String is empty ("") or null.</p>
*
* <pre>
* StringUtils.isEmpty(null) = true
* StringUtils.isEmpty("") = true
* StringUtils.isEmpty(" ") = false
* StringUtils.isEmpty("bob") = false
* StringUtils.isEmpty(" bob ") = false
* </pre>
*
* <p>NOTE: This method changed in Lang version 2.0.
* It no longer trims the String.
* That functionality is available in isBlank().</p>
*
* @param str the String to check, may be null
* @return <code>true</code> if the String is empty or null
*/
public static boolean isEmpty(String str) {
return str == null || str.length() == 0;
}
isBlank(String str)
isBlank(), isNotBlank()
判段字符串是否为空或者null
,空格返回true
/**
* <p>Checks if a String is whitespace, empty ("") or null.</p>
*
* <pre>
* StringUtils.isBlank(null) = true
* StringUtils.isBlank("") = true
* StringUtils.isBlank(" ") = true
* StringUtils.isBlank("bob") = false
* StringUtils.isBlank(" bob ") = false
* </pre>
*/
public static boolean isBlank(String str) {
int strLen;
if (str == null || (strLen = str.length()) == 0) {
return true;
}
for (int i = 0; i < strLen; i++) {
if ((Character.isWhitespace(str.charAt(i)) == false)) {
return false;
}
}
return true;
}
上文讨论的StringUtils
属于package org.apache.commons.lang
;
1.2 字符串判空检查
要了解字符串判空方法的区别首先要理解对象为空字符串""
和 null
的区别
1.2.1 ""
和 null
的区别
null
是没有地址的,可以理解为空指针。当对象在构造器初始化时,如果没有被显示的赋于初值,那么会默认赋值为 null。""
空字符串是一个String
对象是有地址的,只是内容是空。
关于构造器初始化,在没有显示赋予初值的情况下。默认将数值型赋为 0
, 布尔型是 false
,对象引用则是 null
。
String
并不是基本数据类型,而是对象所以会被默认的赋予 null
。
isEmpty()
和isBlank()
区别在于isBlank()
可以多了对于空格的判断,可以根据方法名区别使用isEmpty()
判断字符串是否为空,而isBlank()
则是判断字符串是否是空格,空或null
isEmpty(String str)
isEmpty(), isNotEmpty()
判段字符串是否为空或者null
,空格返回false
/**
* <p>Checks if a String is empty ("") or null.</p>
*
* <pre>
* StringUtils.isEmpty(null) = true
* StringUtils.isEmpty("") = true
* StringUtils.isEmpty(" ") = false
* StringUtils.isEmpty("bob") = false
* StringUtils.isEmpty(" bob ") = false
* </pre>
*/
public static boolean isEmpty(String str) {
return str == null || str.length() == 0;
}
isBlank(String str)
isEmpty(), isNotEmpty()
判段字符串是否为空或者null
,空格返回true
/**
* <p>Checks if a String is whitespace, empty ("") or null.</p>
*
* <pre>
* StringUtils.isBlank(null) = true
* StringUtils.isBlank("") = true
* StringUtils.isBlank(" ") = true
* StringUtils.isBlank("bob") = false
* StringUtils.isBlank(" bob ") = false
* </pre>
*/
public static boolean isBlank(String str) {
int strLen;
if (str == null || (strLen = str.length()) == 0) {
return true;
}
for (int i = 0; i < strLen; i++) {
if ((Character.isWhitespace(str.charAt(i)) == false)) {
return false;
}
}
return true;
}
上文讨论的 CollectionUtils
属于package org.apache.commons.collections
;
1.3 集合判空检查
要了解集合判空方法的区别首先要理解对象为size == 0
和 null
的区别
1.3.1 size==0
和 null
的区别
null
是没有地址的,可以理解为空指针。当对象在构造器初始化时,如果没有被显示的赋于初值,那么会默认赋值为null
。size==0
表示集合已经指向一个地址,但是指向的对象中没有元素。
isEmpty(Collection coll)
isEmpty(), isNotEmpty()
判段集合是否为null
或者不包含任何元素,null
返回true
public static boolean isEmpty(Collection coll) {
return (coll == null || coll.isEmpty());
}
1.4 总结
1.4.1 StringUtils
StringUtils
工具类是用来做字符串的操作的,小菜知道的有两种,org.apache.commons.lang3
包下的StringUtils
类和org.springframework.util
包下的StringUtils
类,org.apache.commons.lang3
包下的StringUtils
类常用的判空有两种:isEmpty
和isBlank
。下面分别介绍一下。
1.org.apache.commons.lang3和org.springframework.util
包中的StringUtils
类的区别:
org.apache.commons.lang3包下的StringUtils
类,判断是否为空的方法参数是字符序列类,也就是string
类型
org.springframework.util
包下的参数是object
类,也就是不仅能判断string
类型,还能判断其他类型,如long
等类型(此包下的更实用)
2.isEmpty
和isBlank
的区别:
这两个方法都是判断字符是否为空的,前者要求没有任何字符,后者要求是空白字符,isBank
判断的空字符其实包括了isEmpty
,也就是说isEmpty
判断的范围比isBank
小。而两者最大的区别就在于——isBank
能判空格符而isEmpty
不能。
1.4.2 CollectionUtils
CollectionUtils
是org.apache.commons.collections
包中的工具类,提供了很多对集合的操作方法。
//(1)判空
CollectionUtils.isNotEmpty(resultApply)
//(2)并集
CollectionUtils.union(listA,listB);
//(3)交集
CollectionUtils.intersection(listA,listB);
//(4)补集
CollectionUtils.disjunction(listA,listB);
//(5)差集
CollectionUtils.subtract(listA,listB);
//(6)是否相等
CollectionUtils.isEqualCollection(listA,listB);