Reputation: 79
I have one collection defined this way, without a specified size -
private final static Collection<String> mycollection = new ArrayList<String>();
static {
mycollection.add("mystr");
}
There is also a constructor that takes a size, e.g.
private final static Collection<String> mycollection = new ArrayList<String>(1);
static {
mycollection.add("mystr");
}
Since the Collection is final, should I be constructing it so that it is a specific size?
Upvotes: -1
Views: 4993
Reputation: 85789
The former uses empty ArrayList
constructor with an initial default capacity of 10 (refer to the previous link), while the latter uses ArrayList(int)
constructor and you will set the initial capacity.
Do we have any memory or performance impact if i initialize.
If saving at least 9 bytes for initial array configuration when having 256 MBs to use, then I would say no.
If you're worried about the initial capacity of the internal array
used by the ArrayList
, here are two excellent Q/As on the subject:
Upvotes: 0
Reputation: 49422
Setting the initial size of an ArrayList
, reduces the number of times the re-allocation of internal memory has to occur. If you create an ArrayList
without setting capacity within constructor it will be created with a default value, which I guess is 10. ArrayList
is a dynamically re sizing data structure, implemented as an array with an initial (default) fixed size. If you know your upper bound of items, then creating the array with initial length is better I suppose.
As per the documentation of ArrayList() constructor :
Constructs an empty list with an initial capacity of ten.
Whereas , ArrayList(int initialCapacity)
Constructs an empty list with the specified initial capacity.
Since the Collection is final, should I be constructing it so that it is a specific size?
The reference variable is final
, it can only point to one object , in this case the ArrayList
. This doesn't imply that the contents or properties of ArrayList
itself cannot be changed . Refer JLS 4.12.4
Once a final variable has been assigned, it always contains the same value. If a final variable holds a reference to an object, then the state of the object may be changed by operations on the object, but the variable will always refer to the same object.
Upvotes: 3
Reputation: 283
The second way will save you a bit memory (the default initial capacity is equal to ten). Assuming you won't alter the list's contents (the underlying array will grow while you add new elements to it).
Notice that the collection isn't immutable (!), only the reference is final. For an immutable list, use Collections.unmodifiableList
or Collections.unmodifiableCollection
methods like this:
private final static Collection<String> mycollection;
static {
List<String> tempList = new ArrayList<String>(1);
tempList.add("mystr");
mycollection = Collections.unmodifiableCollection(tempList);
}
Upvotes: 0
Reputation: 3168
By detault, ArrayList creates and array of 10 elements in it's internal data structure if you do not pass any argument in it.
However, if you paas initialCapacity argument to it you are assigning an initial value to it, which may increase performance at time when you know in advance what will be the size of ArrayList.
public ArrayList(int initialCapacity)
So, in your case if there is only one element it does not makes any difference, but it the list is going to increase more, lowering the initial capacity will make it to recreate the array again.
Upvotes: 0
Reputation: 3992
As the official JavaDoc says:
public ArrayList(int initialCapacity)
Constructs an empty list with the specified initial capacity.
public ArrayList()
Constructs an empty list with an initial capacity of ten.
So if you're not planning to add more elements, the first approach requires less memory. If you're going to add more elements to the collection, however, the second approach does not require re-allocating a new backing array as soon.
Upvotes: 1