-1

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?

Joel
  • 4,732
  • 9
  • 39
  • 54
user2577756
  • 79
  • 1
  • 7
  • If you have a fixed size and it's going to be `final`, why not an array? – Fritz Jul 12 '13 at 20:07
  • http://docs.oracle.com/javase/7/docs/api/java/util/ArrayList.html#ArrayList(int) - seriously, reading the Javadoc kinda explains the difference. – Brian Roach Jul 12 '13 at 20:08

5 Answers5

3

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.

AllTooSir
  • 48,828
  • 16
  • 130
  • 164
  • If you keep adding elements, the internal `array` of the `ArrayList` will grow. It's just a matter of taste which one to use. If this would be a production code, you will only save 9 buckets in the initial array. – Luiggi Mendoza Jul 12 '13 at 20:08
1

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.

Michael Lang
  • 3,902
  • 1
  • 23
  • 37
0

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:

Community
  • 1
  • 1
Luiggi Mendoza
  • 85,076
  • 16
  • 154
  • 332
0

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);
}
MarcelK
  • 283
  • 2
  • 4
  • 11
0

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.

Ankit Zalani
  • 3,068
  • 5
  • 27
  • 47