The capacity of ArrayList and the size of the array in Java
1 Overview
Java allows us to create fixed-size arrays or use collection classes to accomplish similar tasks.
In this tutorial, we will study ArrayList
the difference between the capacity and the size of the Array.
We will also look at some examples to illustrate when ArrayList
and the pros and cons of memory usage.
2.Examples
To understand the difference, let us first try two options.
2.1. Array size
In Java, the size of the array must be specified when creating a new instance of the array:
Integer[] array = new Integer[100];
System.out.println("Size of an array:" + array.length);
Here, we created an I
nteger
array of size 100, which resulted in the following output
Array size: 100
2.2. ArrayList
capacity
Now, let's create an initial capacity of 100 ArrayList
List<Integer> list = new ArrayList<>(100);
System.out.println("Size of the list is :" + list.size());
The size of the list is: 0
Since no elements have been added yet, the size is zero.
Now, let's add an element to the list and check its size:
list.add(10);
System.out.println("Size of the list is :" + list.size());
The size of the list is: 1
3.Array size andArrayList
ArrayList.
Some major differences between capacities.
3.1. Size modification
The array is of fixed size. Once we int
initialize the array with some value as its size, it cannot be changed. The size and capacity are also equal to each other.
ArrayList
The size and capacity are not fixed. The logical size of the list changes according to the insertion and deletion of elements. This is managed separately from its physical storage size. ArrayList
At the threshold of capacity, it will increase its capacity to make room for more elements.
3.2. Memory allocation
Array memory is allocated when it is created. When we initialize the array, it allocates memory based on the size and type of the array. null
Value initializes all elements, and for primitive types, it uses default values to initialize.
ArrayList
**Varies with the growth of memory allocation. **When we ArrayList
specify the capacity during initialization , it will allocate enough memory to store objects that reach that capacity. The logical size remains at 0. It's time to expand the capacity, create a new, larger array, and copy the values into that array.
ArrayList
Objects, there is a special singleton 0 size array, which makes it very cheap to create them. It is also worth noting that ArrayList
the array of object references is used internally.
4.When to use capacityArrayList
ArrayList
When we need to know its size before, we may want to initialize its capacity, but this is usually not necessary. However, for some reasons, this may be the best option.
4.1. Build a largeArrayList
When we know that the list will become larger, it is best to initialize the list with the initial capacity. This prevents some costly growth operations when adding elements.
Similarly, if the list is large, the autogrow operation may allocate more memory than the exact maximum size required. This is because so far, the amount of each increase is calculated in proportion to its size. Therefore, for large lists, this may cause a waste of memory.
4.2. Build smallArrayList
If we have many small collections, then ArrayList
the automatic capacity may provide a large part of wasted memory. Suppose that ArrayList
a size of 10 and a smaller number of elements is preferred, but we only store 2 or 3. This means that 70% of the memory is wasted, which may be important if we have a large number of these lists.
Pre-setting the capacity can avoid this situation.
5.Avoid waste
We should note that ArrayList
is a good solution, it can support random access. It consumes more memory than the array, but provides a richer set of operations.
In some use cases, especially around large collections of primitive values, standard arrays may be faster and use less memory.
Similarly, LinkedList
performance may be higher for storing a variable number of elements that do not need to be accessed through an index . It will not bring any memory management overhead.
6.Summary
In this short article, we saw the ArrayList
difference between capacity and array size. We also studied when it should be ArrayList
used and its benefits in terms of memory usage and performance.
0 Comments