Collection is often used in development (take list as an example). It will be judged to be empty before processing. If it is empty, it will not continue. There are two ways to write it
First of all
List<String> listA = new ArrayList<>();
if(listA.isEmpty()){
System.out.println(listA);
}
The second is that
List<String> listB = null;
if(null != listB && listB.size() == 0){
System.out.println(listB);
}else{
System.out.println("listB is NULL");
}
Check the isempty() method source code of ArrayList
As you can see, lista.Isempty() is just looking at the size of the collection, null= Listb is to determine whether the set is null and allocate memory for it
Therefore, it is suggested that when it is not clear whether the set is null, the second method should be used to judge whether the set is empty
Similar Posts:
- Java container — unsupported operation exception
- [Solved] Exception in thread “main” java.util.ConcurrentModificationException
- Convert Object to List>, avoiding Unchecked cast: ‘java.lang.Object’ to ‘java.util.List
- How to Solve Java Error: java.util.Arrays$ArrayList cannot be cast to java.util.ArrayList
- [leetcode] 140. Word break II word split II
- [Solved] WordPress Error: Allowed memory size of 134217728 bytes exhausted
- An error is reported when traversing the list collection to delete elements
- 140. Word Break II
- How to Solve Request processing failed; nested exception is java.lang.NullPointerException
- How to Solve Console error: index: 0, size: 0