In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article introduces the relevant knowledge of "what is the difference between empty list sets or null sets in Java". In the operation of actual cases, many people will encounter such a dilemma. Next, let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
The difference between an empty list collection or a null collection
JavaWeb project, a null pointer is reported in the background when the list collection has no data when a third-party system is called to obtain data from a third-party database.
Discover the cause
When the total number of records in the database queried in the background is 0 (the database really has no data), I directly set the query object collection list to null and return it to the page.
Solution method
When the total number of records in the database queried by the backend is 0 (the database really has no data), new the list again and return it to the page.
In this way, the background console will not report a null pointer.
If (listings ordered null & &! list.isEmpty ()) {/ / take the value in list} else {/ / do other processing}
Judging in this way, I found that when the collection is null, although a collection is re-new, the background still reports a null pointer.
It would be better for me to judge like this after that.
If (listings invalid null & & list.size ()! = 0 {/ / take the value in list} 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 or not
In Java, whether the list collection is empty (there are no elements in the collection) or null are two different things.
For example, I have an empty water cup (list), but you do not, then you are null, my size is 0. If you want to hold water, you need to buy a water cup yourself (new ArrayList ();), but I can fill it directly (list.add). If you pour water directly without a cup, the water will flow out (the null pointer is abnormal).
two。 So, when do we use null, and when do we use isEmpty () or list.size ()?
IsEmpty () or (list.size () = = 0) is used to determine whether the List content is empty, that is, there are no elements in the set, but the premise of using isEmpty () and size () is that list is an empty collection, not null, so in order to avoid exceptions, it is recommended to do an empty collection creation process to allocate memory space before using or assigning the list collection, that is, List list = new ArrayList ()
There is no difference between 3.list.isEmpty () and list.size () = = 0
IsEmpty () determines whether there are elements, while size () returns several elements. If you determine whether a set has elements or not, it is recommended to use the isEmpty () method, which looks clear.
4.list equals null, which means that no memory space is allocated to the list collection, which actually doesn't exist at all.
So the general judgment is that
If (listings empty null & &! list.isEmpty ()) {/ / take the value in list} else {/ / do other processing} to determine whether the List collection is empty or null
Determine whether the List collection is empty
In Java, judging whether the List collection is empty is not the same as whether it is null
Create a new List object, which is empty by default, that is, no data, not null
Such as:
List list = new ArrayList (); list1.size () = = 0 and isEmpty () judge that the content in list1 is empty, not an empty collection, and the list2 declared by nullList list2 = null; is null
Test whether list is empty or not
List list1 = new ArrayList (); 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 list2=null; System.out.println (list2 + "= > list2=null")
The output is:
01
four
Test list==0
Null== > list2=null
You can get that list1.size () is 0, which can be understood as allocating memory space to 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.
However, list2 has no water cup and null pointer is abnormal.
Two conditions for judging by List set
If (null = = list | | list.size () = = 0) {}
1. Judge null = = list, judge whether there is this container water cup
2.list.size () = = 0 to determine whether there is water in the container water cup
3.list.size () and list.isEmpty () have the same meaning.
This is the end of the content of "what is the difference between an empty list collection or a null in Java". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.