Priyank Doshi
Priyank Doshi

Reputation: 13161

Why is the default capacity of ArrayList 10?

I saw the java doc for ArrayList and found that the initial capacity of ArrayList is 10.

 /**
 * Constructs an empty list with an initial capacity of ten.
 */
public ArrayList() {
this(10);
}

I think it would make sense if it were any power of 2, but why 10?

I also checked HashMap's initial capacity, and it's 16 which makes sense.

/**
 * The default initial capacity - MUST be a power of two.
 */
static final int DEFAULT_INITIAL_CAPACITY = 16;

/**
 * Constructs an empty <tt>HashMap</tt> with the default initial capacity
 * (16) and the default load factor (0.75).
 */
public HashMap() {
    this.loadFactor = DEFAULT_LOAD_FACTOR;
    threshold = (int)(DEFAULT_INITIAL_CAPACITY * DEFAULT_LOAD_FACTOR);
    table = new Entry[DEFAULT_INITIAL_CAPACITY];
    init();
}

Is there any specify reason behind the number 10?

Upvotes: 37

Views: 51517

Answers (7)

Shubhamhackz
Shubhamhackz

Reputation: 7973

ArrayList is just an Array which can grow automatically..

Yes..the default size is 10

and I think that there's not much thinking behind this initial/default value.The default value 10 just seems not loo large and also not too small.(May be this could be reason). What if you exceed the default initial capacity of the array..? The next capacity for the array is calculated by-

New capacity=(current capacity*3)/2+1
So next size would be (10*3)/2+1= 16
And next (16*3)/2+1= 25
And So on...

Upvotes: 0

Denham Coote
Denham Coote

Reputation: 674

Vector, from JDK 1.0 had a default initial capacity of 10 so it probably made some sense to remain consistent when they introduced ArrayList in 1.2.

Upvotes: 10

Stephen C
Stephen C

Reputation: 719089

Unless there is a comment in the code, we'll never know for sure. However, I imagine that at some point a Sun engineer has gathered statistics on ArrayList usage over a large number of real-world applications, and determined ... empirically ... that 10 gave roughly the best results on average. (That's how they tune things like this, the optimizer, the bytecode design and so on.)

And, and others pointed out, there is no computational advantage (or disadvantage) in using a size that is a power of two for the size of an ArrayList.

Upvotes: 0

Stepan Vihor
Stepan Vihor

Reputation: 1070

The ArrayList is simple growing array. When trying to add element, and the buffer size is exceeded, it is simply growing. So the initial size can be any positive value.

The 1 would be too little. Even with a few elements we will have a few resize operations.

The 100 would be a loss of space.

So, the 10 is compromise. Why 10 and not 12 or 8? First hint, that the typical use cases were analysed and this is the best fit between lost of performance and lost of space. However, I think, seeing the Sun's original code, that it wasn't analysed so deeply and it is an arbitrary 'not too small, not too big' number.

Upvotes: 42

Michael Borgwardt
Michael Borgwardt

Reputation: 346347

For a List, there is no advantage in having the capacity be a power of two. In fact, there is no real advantage in any specific starting capacity. It has to be large enough to avoid multiple resizing steps for the common case of small lists, and small enough not to waste memory on unused capacity in that same case. 10 was probably chosen simply because it falls in the right range to fulfill these requirements and because it's "round".

Upvotes: 13

Thilo
Thilo

Reputation: 262644

Completely arbitrary choice.

And there is no reason why power-of-2 makes any more sense here. It makes sense in a HashMap, because of how the hashing works. In fact, it has to be a power of two (according to the comment in the source).

Note that java.util.Vector (which is the older brother of ArrayList) also has 10.

Upvotes: 5

Pramod Kumar
Pramod Kumar

Reputation: 8014

10 is probably a more or less arbitrary number for the default number of elements.

Upvotes: 1

Related Questions