java - Is there any performance/memory benefit if the capacity of Array List is being specified which is holding only two elements? -


i've array list going hold 2 elements, want specify initial capacity 2 since initial capacity ten default.

list<integer> values = new arraylist<integer>(2); 

will performance/memory benefit out of it?

any discussions around appreciated...

you not performance benefit out of it, except small reduction in memory usage.

if you're sure size 2 elements , never change, , obtain bit of performance boost, use array of primitive types (unless there's reason prefer integer, int better option):

int[] values = new int[2]; 

update

if need store mixed types, use object[]. it's still better alternative using arraylist, if size fixed 2 elements:

object[] values = new object[2]; 

Comments