7

I was wondering, which way of initialization of a list is better?


public class Main {
    private final List<String> l = new ArrayList<String>();

    {
        l.add("a");
        l.add("b");
        l.add("c");
    }
}

public class Main {

    private final List<String> l = new ArrayList<String>() {{
        l.add("a");
        l.add("b");
        l.add("c");        
    }};
}

Cheok Yan Cheng
  • 47,586
  • 132
  • 466
  • 875
  • 2
    Latter works, but you don't have to use `l.`-prefix in body. Plain `add("a");` is enough. – Ahe Feb 03 '10 at 07:36

3 Answers3

6

I prefer using static factory methods of the next kind:

public final class CollectionUtils {
    private CollectionUtils() {  
    }

    public static <T> List<T> list(T... data) {
        return Arrays.asList(data);
    }

    public static <T> ArrayList<T> newArrayList() {
        return new ArrayList<T>();
    }

    public static <T> ArrayList<T> newArrayList(T... data) {
        return new ArrayList<T>(list(data));
    }
}

So, you can use them in your code in the next way:

import static CollectionUtils.list;
import static CollectionUtils.newArrayList;

public class Main {
    private final List<String> l1 = list("a", "b", "c");
    private final List<String> l2 = newArrayList("a", "b", "c");
}

Thus you get relatively compact way of creating and populating lists and don't need to duplicate generic declarations. Note, that list method just creates list view of array. You cannot add elements to it later (or remove). Meanwhile newArrayList creates usual ArrayList object.

As Joachim Sauer noted, these utility methods (and many other useful things) can be found in Google Collections library (which is now a part of Guava project).

Community
  • 1
  • 1
Rorick
  • 8,857
  • 3
  • 32
  • 37
  • 1
    I have to vote this one up, since this is the approach I've used on several projects. However, I think you forgot the static imports in the 'Main' example :) – Christoffer Feb 03 '10 at 08:14
  • IDE usually adds them for me) – Rorick Feb 03 '10 at 08:19
  • 2
    Google Collections does this (and a lot of other things) for you. Those factory methods are in the `Lists` class for example. – Joachim Sauer Feb 03 '10 at 08:31
  • I know, but it contains too much stuff for me. Nevertheless, good point, I add it to answer. – Rorick Feb 03 '10 at 08:58
  • Thanks for the Google Collections information. Good practice keep evolve over time :) – Cheok Yan Cheng Feb 03 '10 at 17:00
  • Guava (Google Collections) has JAR file of approx. 2MB. When I add it to my project, deployment takes few seconds longer (due to parsing that JAR file each time) and due to glassfish memory leaks/problems with redeploy makes me turn glassfish more often. There is no need to use it if you need only several classes, rather makes your own utility classes. That's what I did and I vote up this answer. – Mladen Adamovic Feb 26 '14 at 15:37
4

the former, since it doesn't create an anonymous class; check the reasons here

Community
  • 1
  • 1
dfa
  • 114,442
  • 31
  • 189
  • 228
2

Neither, because none is making the list immutable. I guess you want to make it immutable, when you use final. If thats not the case, I apologize for my assumption.

If I am correct in assuming that, you should have a look into Collections.unmodifiableList(). And the latter will come handy,

private final List<String> l = Collections.unmodifiableList(new ArrayList<String>() {{
        add("a");
        add("b");
        add("c");        
}});

Otherwise, dfa is correct in suggesting the former.

Another way can be this,

private final List<String> l = Collections.unmodifiableList(Arrays.asList("a", 
                                    "b", "c"));
Adeel Ansari
  • 39,541
  • 12
  • 93
  • 133
  • The second example is final so `l` can be accessed inside the anonymous block I assume. That's misguided because you don't need it and you could just write `add("a")` instead of `l.add("a")` but I assume that's the reason rather than pseudo-immutability. – cletus Feb 03 '10 at 07:37