Home > Java > JavaBase > Java method to determine whether it is empty

Java method to determine whether it is empty

Release: 2019-12-21 17:07:43
Original
2673 people have browsed it

Java method to determine whether it is empty

Java method to determine whether it is empty:

1. Determine whether a string or object is empty

Judgment of StringUtils

StringUtils.isEmpty(CharSequence cs); //StringUtils class under the org.apache.commons.lang3 package, the method parameter to determine whether it is empty is the character sequence class, which is the String type

StringUtils.isEmpty(Object str); //The parameter under the org.springframework.util package is the Object class, which means it can not only determine the String type, but also other types, such as Long and other types.

StringUtils.isEmpty(CharSequence cs) source code of org.apache.commons.lang3:

public static boolean isEmpty(final CharSequence cs) { 
       return cs == null || cs.length() == 0;
}
Copy after login

StringUtils.isEmpty(Object str) source code of org.springframework.util :

public static boolean isEmpty(Object str) { 
       return (str == null || "".equals(str));
}
Copy after login

Basically determine whether the object is empty, StringUtils.isEmpty(Object str) can do this.

2. Determine whether the array is empty

list.isEmpty(); //返回boolean类型。
Copy after login

3. Determine whether the collection is empty

CollectionUtils.isEmpty(null): true
CollectionUtils.isEmpty(new ArrayList()): true
CollectionUtils.isEmpty({a,b}): false
Copy after login

More java knowledge Please pay attention to the java basic tutorial column.

The above is the detailed content of Java method to determine whether it is empty. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template