In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-10 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article shows you the three ways of writing whether the java return set is null or empty collection and empty collection respectively, the content is concise and easy to understand, it can definitely brighten your eyes. I hope you can get something through the detailed introduction of this article.
Three ways to write whether the return set is null or empty set and empty set
I think that when I write an interface, I will return an empty collection when I need to return a collection. For example, if a mybatis query returns a collection, if the result is empty, it will return an empty collection instead of null.
So what's the advantage of this? The biggest advantage is that the caller does not have to judge whether it is null or not, but can use it directly because there is no need to sell the pointer short.
There are drawbacks, of course, if you return Lists.newArrayList (); or newArrayList (); this creates a new object, which is probably unnecessary, which is a waste of performance.
Of course, there is also a solution, you can use Collections.emptyList (); this method returns an empty collection and does not create a new object, but returns
Public static final List EMPTY_LIST = new EmptyList ()
This variable.
Of course, this also has its drawbacks. If the caller is just traversing, it will not report an error, but if you want to add it and delete the elements in it, you will report an error.
So you may wonder why, the reason is that there is a mistake in calling the Times directly in the code, so why write it this way?
The reason is also very simple. If multiple threads add or delete this collection, then the caller will be completely confused, so the method of reporting errors directly and failing quickly will be adopted.
To solve the problem.
Summary:
Return null, return new ArrayList (), return EMPTY_LIST.
Null is definitely not recommended, so should you create a new List or return an empty List?
This should be based on the performance requirements of the interface, and return EMPTY_LIST if the performance requirements are high, otherwise create a new object.
The first way to return an empty List: new ArrayList ()
JDK1.8 has been optimized. The list internal shared empty array created by the default constructor will only be expanded to the default capacity when data is inserted for the first time.
Private static final Object [] DEFAULTCAPACITY_EMPTY_ELEMENTDATA = {}; public ArrayList () {this.elementData = DEFAULTCAPACITY_EMPTY_ELEMENTDATA;} mode II: new ArrayList (0) private static final Object [] EMPTY_ELEMENTDATA = {}; public ArrayList (int initialCapacity) {if (initialCapacity > 0) {this.elementData = new Object [initialCapacity];} else if (initialCapacity = = 0) {this.elementData = EMPTY_ELEMENTDATA;} else {throw new IllegalArgumentException ("Illegal Capacity:" + initialCapacity);}} mode III: Collections.emptyList () (recommended)
Features: immutable, safe
/ * The empty list (immutable). This list is serializable.** @ see # emptyList () * / @ SuppressWarnings ("rawtypes") public static final List EMPTY_LIST = new EmptyList (); / * * Returns an empty list (immutable). This list is serializable.**
This example illustrates the type-safe way to obtain an empty list:* * List s = Collections.emptyList (); * * @ implNote* Implementations of this method need not create a separate List* object for each call. Using this method is likely to have comparable* cost to using the like-named field. (Unlike this method, the field does* not provide type safety.) * * @ param type of elements, if there were any, in the list* @ return an empty immutable list** @ see # EMPTY_LIST* @ since 1.5*/@SuppressWarnings ("unchecked") public static final List emptyList () {return (List) EMPTY_LIST;} / * * @ serial include*/private static class EmptyListextends AbstractListimplements RandomAccess, Serializable {private static final long serialVersionUID = 8842843931221139166L leading public Iterator iterator () {return emptyIterator () } public ListIterator listIterator () {return emptyListIterator ();} public int size () {return 0;} public boolean isEmpty () {return true;} public boolean contains (Object obj) {return false;} public boolean containsAll (Collection c) {return c.isEmpty ();} public Object [] toArray () {return new Object [0];} public T [] toArray (T [] a) {if (a.length > 0) a [0] = null;return a } public E get (int index) {throw new IndexOutOfBoundsException ("Index:" + index);} public boolean equals (Object o) {return (o instanceof List) & & ((List) o) .isEmpty ();} public int hashCode () {return 1;} @ Overridepublic boolean removeIf (Predicate
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.