The list collection in Java is empty

1. If you want to judge whether the list is empty, you can judge as follows:

if(null == list || list.size() ==0 ){

  // empty case

}else{

  //if not empty

}

  

2. What is the difference between list.isEmpty() and list.size()==0?

    Answer: There is no difference.

      isEmpty() determines whether there are elements, and size() returns how many elements there are.

      If it is recommended to use the isEmpty() method to determine whether a collection has elements, it is more logical to use.

3. List! =null followed by ! What is the difference between list.isEmpty()?

      This is quite similar to, if you want to drink water,
      the front is to judge whether there is not even a water cup, and the
      latter is to judge whether there is no water in the water cup, not
      even anything that holds water. Where does
      this water come from?
      So the general judgment is
      

      if(list!=null && !list.isEmpty()){
        this takes the value in the list
      }else{
        do other processing
      }

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325300778&siteId=291194637