The role and benefits of Java Collections.emptylist (), Emptyset (), Emptymap (), and the areas to be noted

Source: Internet
Author: User
Tags throw exception

Tag: map min Adds add htm without tar size call

Transfer from https://www.cnblogs.com/qiumingcheng/p/7126281.html

Let's start by explaining what the benefits are:
1, if you want to new an empty list, and this list will not add elements later, then use Collections.emptylist () good.
New ArrayList () or new LinkedList () will have an initial size at the time of creation and will occupy a memory.
Every use of new an empty list collection, waste is a big one, waste is serious, it is not good
2, in order to encode the convenience.
For example, a method return type is list, and when there is no result, returns NULL, returns the list of lists when there is a result.
In that case, a null judgment is required where this method is called. Using a method like Emptylist, you can make it easy for the method caller. The returned will not be null, eliminating the duplication of code.

Note the place:
This empty collection is not called. Add (), add element. Because the direct report is abnormal. Because the source code is so written: direct throw exception.

Well, collections didn't write that, but Emptylist inherited Abstractlist, a simple way to implement a partial set of frames.
The method in which the Add method is finally called is the direct throw exception.
throw new Unsupportedoperationexception ();
This explains the exception to the Add report.

The role and benefits of Java Collections.emptylist (), Emptyset (), Emptymap (), and the areas to be noted

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.