Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there any case when I should use ensureCapacity() on ArrayList externally?

The below code ensures a capacity of 11 internally,

ArrayList list = new ArrayList(11);

so why/when should I use public method ensureCapacity() externally?

list.ensureCapacity(11);

And if there is no use why it is kept public?

public void ensureCapacity(int minCapacity) {
    int minExpand = (elementData != DEFAULTCAPACITY_EMPTY_ELEMENTDATA)
        // any size if not default element table
        ? 0
        // larger than default for default empty table. It's already
        // supposed to be at default size.
        : DEFAULT_CAPACITY;

    if (minCapacity > minExpand) {
        ensureExplicitCapacity(minCapacity);
    }
}
like image 757
Milind Vinkar Avatar asked Jul 04 '16 09:07

Milind Vinkar


People also ask

What does ArrayList ensureCapacity do?

ensureCapacity. Increases the capacity of this ArrayList instance, if necessary, to ensure that it can hold at least the number of elements specified by the minimum capacity argument.

How does ensureCapacity work in Java?

The ensureCapacity() method of StringBuffer class ensures the capacity to at least equal to the specified minimumCapacity. If the current capacity of StringBuffer < the argument minimumCapacity, then a new internal array is allocated with greater capacity.

What is the difference between ADD () and Set () when using ArrayList?

ArrayList set() Vs. However, there is a major difference between them. The set() method adds a new element at the specified position by replacing the older element at that position. The add() method adds a new element at the specified position by shifting the older element towards the right position.

What is the use of ensureCapacity () Mcq?

ensureCapacity(3); has manually increased the capacity of obj to 3 but the value is stored only at index 0, therefore obj. size() returns the total number of elements stored in the obj i:e 1, it has nothing to do with ensureCapacity(). 10.


2 Answers

Calling new ArrayList(n) initializes the ArrayList with capacity n.

However, there may be a case where you now want to add a large number of elements to this already created ArrayList and the ArrayList is full. Lets say we want to add x new elements.

It would be more efficient to call list.ensureCapacity(n + x) and then add the elements, than it would be to just add the elements and let the List dynamically resize.

Consider this crude example:

ArrayList<Integer> exampleList = new ArrayList<>(200);

Integer[] exampleArray = new Integer[800];

exampleList.ensureCapacity(1000);

Collections.addAll(exampleList, exampleArray);
like image 172
explv Avatar answered Sep 28 '22 04:09

explv


You might need to use it when you don't want to resize the arrylist automatically and want to keep control to yourself. e.g. you created an ArrayList of 5 size. If you add 6th element it will create a space for 5+5 i.e 10 elements. If you dont want your arryList to double the size you can use list.ensureCapacity to tell how many elements you need.

This is fine for small data but assume your arrayList grows to 5K.

As per java documentation https://docs.oracle.com/javase/7/docs/api/java/util/ArrayList.html :

An application can increase the capacity of an ArrayList instance before adding a large number of elements using the ensureCapacity operation. This may reduce the amount of incremental reallocation.
like image 33
sauumum Avatar answered Sep 28 '22 05:09

sauumum