Home > Java > javaTutorial > How to determine whether list is empty in java?

How to determine whether list is empty in java?

青灯夜游
Release: 2019-12-31 13:43:56
Original
4329 people have browsed it

How to determine whether list is empty in java?

Java method to determine whether the collection list is empty:

If you want to determine whether the list is empty , it can be judged like this:

if(list == null || list.size() ==0 ){
  //为空的情况
}else{
  //不为空的情况
}
Copy after login

The difference between list.isEmpty() and list.size()==0

Answer: No difference . isEmpty() determines whether there are elements, and size() returns how many elements there are. If you determine whether a collection has elements, it is recommended to use the isEmpty() method. It is more logical usage.

isEmpty() also determines whether there are elements in the list collection. If there are elements, it returns false. If there is no element, it returns true. If the collection itself is set to null, a null pointer exception will be reported.

list! =null Follow ! What is the difference between list.isEmpty()?

This is equivalent to if you want to go to the store to buy something

list! =null First determine whether there is a store

! list.isEmpty() does not determine whether the store exists, but determines whether the store has items

Summary usage: If there is no store, how can anything be sold?

So the general judgment It’s

if(list!=null && !list.isEmpty()){
   //不为空的情况
}else{
   //为空的情况
}
Copy after login

Recommended learning: Java video tutorial

The above is the detailed content of How to determine whether list is empty in java?. 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