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