JAVA set to determine the null: List.isEmpty() & null == List && List.size()==0

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: