Explanation of the difference between list set being empty or null in Java
- 2021-12-12 04:14:48
- OfStack
The difference between an empty list set and an null set
javaWeb project, when the third party system is called to obtain the data of the third party database, when the list set has no data, the background reports a null pointer
Discover the cause
When the total number of records in the background query database is 0 (the database really has no data), I directly set the query object set list to null, and then return it to the page.
Solution
When the total number of records in the background query database is 0 (the database does not have data), list is re-new1, and then it is returned to the page.
In this way, the background console will not report a null pointer
if(list!=null && !list.isEmpty()){
// Take this from inside list Value in
}else{
// Do other processing }
Judging from this, I found that when the set is null, although new is re-created by 1 set, the background will still report a null pointer
After that, I'll just judge like this
if(list!=null && list.size()!=0{
// Take this from inside list Value in
}else{
// Do other processing }
This problem arises because I don't have a deep understanding of whether the Java List collection is empty or null.
I record the following points
1. Determine whether an list set is empty
In Java, whether the list collection is empty (there are no elements in the collection) or null is a different matter.
For example, if I have an empty water cup (list) and you don't, then you are null and my size is 0. If you want to fill water, you need to buy your own water cup (new ArrayList ();) But I can just load the water (list. add). If you pour water directly without a cup, the water will flow out (null pointer is abnormal).
2. So, when do we use null, when do we use isEmpty () or list. size ()?
isEmpty () or (list. size () = = 0) is used to judge whether the content of List is empty, that is, there is no element in the set. However, the premise of using isEmpty () and size () is that list is an empty set instead of null. Therefore, in order to avoid exceptions, it is recommended to do an empty set creation process and allocate memory space before using or assigning list set, that is, List list = new ArrayList ();
3. There is no difference between list. isEmpty () and list. size () = = 0
isEmpty () determines whether there are elements, while size () returns several elements. If you determine whether there are elements in a collection, it is recommended to use isEmpty () method, which looks clear and clear.
4. list is equal to null, which can be understood as no memory space is allocated to list collection, but it actually does not exist at all.
So the general judgment is
if(list!=null && !list.isEmpty()){
// Take this from inside list Value in
}else{
// Do other processing
}
Judge whether the List set is empty or null
Determine whether the List set is empty
In the ES 116EN, whether the ES 117EN set is empty or not is different from whether the ES 118EN set is empty or not
Create a new List object, which is empty by default, that is, there is no data instead of null
Such as:
List<User> list = new ArrayList<User>();
list1.size()==0 And isEmpty() The judgment is that list1 The contents inside are empty instead of 1 An empty collection, null
List list2 = null;
Declared in this way list2 For null
Test whether list is empty
List<User> list1 = new ArrayList<User>();
if (list1 != null) {
System.out.println(list1.size() + "1");
}
if (list1.size() > 0) {
System.out.println("2");
}
if (null == list1) {
System.out.println("3");
}
if (list1.size() == 0) {
System.out.println("4");
}
if (list1 != null && list1.size() == 0) {
System.out.println("test list==0");
}
if (list1 != null && list1.size() > 0) {
System.out.println("test list>0");
}
List<User> list2 = null;
System.out.println(list2 + "==>list2=null");
Output yields:
01
4
test list==0
null== > list2=null
You can get list1.size () to be 0, which can be understood as allocating memory space for the list collection, but the data is empty
While list2 is null, the system does not have weiqi to allocate space for it
There is an example on the Internet, list1 can be compared to an empty water cup, there is no water, so size is 0
While list2 has no water cup, the null pointer is abnormal
Two Conditions for List Set Judgment
if(null == list || list.size() ==0 ){
}
1. Judge null = = list, and judge whether there is this container water cup
2. list. size () = = 0, judge whether there is water in the container cup
3. list. size () and list. isEmpty ()