Get the App
SLTechnology News&Howtos  ›  Development  › 

What are the differences between Collections.EMPTY_LIST and Collections.emptyList () in Java

Shulou Source: shulou.com Published: 2022-06-02 14:45:12 09月10日 Update

This article will explain in detail what is the difference between Collections.EMPTY_LIST and Collections.emptyList () in Java. The editor thinks it is very practical, so I share it with you for reference. I hope you can get something after reading this article.

Difference between Collections.EMPTY_LIST and Collections.emptyList ()

Collections.EMPTY_LIST returns an empty List. Why do you need an empty List? Sometimes we need to return a List in the function, but the List is empty. If we return null directly, the caller also needs to judge the null, so it is generally recommended to return an empty List.

The empty List returned by Collections.EMPTY_LIST cannot perform operations such as adding elements. At this point, you may say, I directly return a new ArrayList (), but new ArrayList () will take up a certain amount of resources during initialization, so in this scenario, it is recommended to return Collections.EMPTY_LIST.

Collections. EmptyList () also returns an empty List, the only difference between it and Collections.EMPTY_LIST is that Collections. EmptyList () supports generics, so when you need generics, you can use Collections. EmptyList ().

Collections.EMPTY_MAP and Collections.EMPTY_SET are the same.

Collections.EMPTY_LIST implementation code / * The empty list (immutable). This list is serializable. * * @ see # emptyList () * / @ SuppressWarnings ("unchecked") public static final List EMPTY_LIST = new EmptyList (); Collections. EmptyList () implementation code / * Returns the empty list (immutable). This list is serializable. * *

This example illustrates the type-safe way to obtain an empty list: * * List s = Collections.emptyList (); * * Implementation note: 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.) * * @ see # EMPTY_LIST * @ since 1.5 * / @ SuppressWarnings ("unchecked") public static final List emptyList () {return (List) EMPTY_LIST;} A strange problem caused by the use of Collections.emptyMap () is the following console information

The second line is a very inconspicuous exception message that somehow did not output the entire error stack.

At first, I didn't notice this exception message, so I set a breakpoint and debugged it. As a result, it was inexplicably transferred to ThreadPoolExecutor for execution every time I executed a few sentences.

Finally, after noticing the exception information above, we found that there was a problem initializing a member variable of type Map:

Protected Map optionalStrParams = Collections.emptyMap ()

It is modified as follows:

Protected Map optionalStrParams = new HashMap ()

My intention was that initializing to an empty Map,EmptyMap would not be appropriate in this scenario.

This is the end of the article on "what is the difference between Collections.EMPTY_LIST and Collections.emptyList () in Java". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, please share it for more people to see.

Tags: Information articles code scenarios suggestions more questions obscure good appropriate practical inexplicable elements content functions variables stacks members console articles Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno macOS MariaDB Apple Linux OPPO Reno