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

Java method to determine whether it is empty

尚
Original
2019-12-21 17:07:432640browse

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;
}

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

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

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

2. Determine whether the array is empty

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

3. Determine whether the collection is empty

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

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!

Statement:
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