Difference between EMPTY_ELEMENTDATA and DEFAULTCAPACITY_EMPTY_ELEMENTDATA in ArrayList

The trigger was I read the source of ArrayList. I wrote what I was interested in I read the source of ArrayList, but I read it further and was interested is.

Difference between EMPTY_ELEMENTDATA and DEFAULTCAPACITY_EMPTY_ELEMENTDATA in ArrayList

"I don't understand the difference between LL.116-126 EMPTY_ELEMENT DATA and DEFAULTCAPACITY_EMPTY_ELEMENTDATA. Do you understand if you read on? However, the latter javadoc properly says, "This is the difference." Such comments are important. But I still don't understand it. 』\

That's right. I know what I want to do, but I don't understand the intention of dividing it into two variables. Well, I was curious and checked it.

First stage

Then, the first stage correction was found. First, lazy evaluation of the default constructor. Certainly, if it is the source of the old JDK, when I look at the constructor, I feel that I immediately understood how much size should be secured by the initial value. It's an empty array. This is the one that fixed it. 8011200: (coll) Optimize empty HashMap and ArrayList

public ArrayList() {
//        this(10);
        super();
        this.elementData = EMPTY_ELEMENTDATA;
}

Instead of allocating 10 arrays at first, make it empty and expand it when added. This is fine.

Second stage

Next, a second-stage fix was found. Here, DEFAULTCAPACITY_EMPTY_ELEMENTDATA is added. 8035584: ArrayList(c) should avoid inflation if c is empty

When an empty list is passed with public ArrayList (Collection <? extends E> c) {, c.toArray (); is called and the number of instances increases steadily. So, when size == 0, I set this.elementData = EMPTY_ELEMENTDATA ;. I understand that.

So why did you change this.elementData = EMPTY_ELEMENTDATA; to this.elementData = DEFAULTCAPACITY_EMPTY_ELEMENTDATA; when public ArrayList () {? Well, I don't know.

What I saw

Judging from both modifications, when new ArrayList () was done, it was originally new ArrayList (10). But now I'm substituting an empty array. On the other hand, when new ArrayList (0) is executed, an empty array is assigned. The same is true when passing an empty list to the constructor.

In other words, when adding for the first time after creating an instance, DEFAULTCAPACITY_EMPTY_ELEMENTDATA and EMPTY_ELEMENTDATA are assigned separately to distinguish whether it was created with new ArrayList () or new ArrayList (0). ..

public void ensureCapacity(int minCapacity) {
    int minExpand = (elementData != DEFAULTCAPACITY_EMPTY_ELEMENTDATA) ? 0 : DEFAULT_CAPACITY;

This difference between 0 and 10 (DEFAULT_CAPACITY) is ignored when ensureCapacity (5) is called because the buffer size is originally 10 for new ArrayList (), and from buffer size = 0 for new ArrayList (0). The decision was to increase the buffer size to 5.

private void ensureCapacityInternal(int minCapacity) {
    if (elementData == DEFAULTCAPACITY_EMPTY_ELEMENTDATA) {
        minCapacity = Math.max(DEFAULT_CAPACITY, minCapacity);

When this max is also new ArrayList (), even if you try to add to buffer size = 0 and set buffer size = 1, it means that buffer size = 10 (DEFAULT_CAPACITY) suddenly.

Conclusion

Don't fix another case together with the fix "ArrayList (c) should avoid inflation if c is empty". Maybe it's written somewhere.

Recommended Posts

Difference between EMPTY_ELEMENTDATA and DEFAULTCAPACITY_EMPTY_ELEMENTDATA in ArrayList
Difference between ArrayList and LinkedList
Difference between List and ArrayList
[Java] Difference between array and ArrayList
Difference between final and Immutable in Java
Difference between getText () and getAttribute () in Selenium
Difference between "|| =" and "instance_variable_defined?" In Ruby memoization
Difference between int and Integer in Java
Difference between vh and%
Difference between i ++ and ++ i
Difference between next () and nextLine () in Java Scanner
Difference between product and variant
Difference between redirect_to and render
[Java] Difference between == and equals
Rails: Difference between resources and resources
Difference between puts and print
Difference between redirect_to and render
About the difference between classes and instances in Ruby
Difference between new and create in Rais action controller
Difference between CUI and GUI
[Java] Difference between static final and final in member variables
Difference between variables and instance variables
Difference between mockito-core and mockito-all
Difference between class and instance
Difference between bundle and bundle install
Difference between render and redirect_to
Difference between .bashrc and .bash_profile
Difference between StringBuilder and StringBuffer
Difference between render and redirect_to
Difference between render and redirect_to
About the difference between "(double quotation)" and "single quotation" in Ruby
Jersey --What is Difference Between bind and bindAsContract in HK2?
Difference between element 0, null and empty string (check in list)
Is short-circuit evaluation really fast? Difference between && and & in Java
[Ruby] Difference between get and post
Difference between instance method and class method
Difference between render method and redirect_to
Difference between interface and abstract class
Difference between == operator and equals method
[Terminal] Difference between irb and pry
JavaServlet: Difference between executeQuery and executeUpdate
Difference between == operator and eqals method
Rough difference between RSpec and minitest
[Rails] Difference between find and find_by
Understand the difference between each_with_index and each.with_index
Difference between instance variable and class variable
[JAVA] Difference between abstract and interface
Difference between Thymeleaf @RestController and @Controller
Difference between Stream map and flatMap
Difference between primitive type and reference type
Difference between string.getByte () and Hex.decodeHex (string.toCharaArray ())
[Java] Difference between Closeable and AutoCloseable
[Java] Difference between StringBuffer and StringBuilder
[Java] Difference between length, length () and size ()
[rails] Difference between redirect_to and render
[Android] Difference between finish (); and return;
Note: Difference between Ruby "p" and "puts"
[Memo] Difference between bundle install and update
Difference between Ruby instance variable and local variable
[For beginners] Difference between Java and Kotlin
Difference between isEmpty and isBlank of StringUtils