Compact syntax for instantiating an initializing collection
Asked Answered
L

7

32

I'm looking for a compact syntax for instantiating a collection and adding a few items to it. I currently use this syntax:

Collection<String> collection = 
    new ArrayList<String>(Arrays.asList(new String[] { "1", "2", "3" }));

I seem to recall that there's a more compact way of doing this that uses an anonymous subclass of ArrayList, then adds the items in the subclass' constructor. However, I can't seem to remember the exact syntax.

Lexis answered 22/9, 2010 at 13:2 Comment(0)
P
48

http://blog.firdau.si/2010/07/01/java-tips-initializing-collection/

List<String> s = Arrays.asList("1", "2");
Patroclus answered 22/9, 2010 at 13:8 Comment(1)
Only works like that if you don't need the result to be extendable, though.Largish
T
14

I guess you're thinking about

collection = new ArrayList<String>() { // anonymous subclass
     { // anonymous initializer
         add("1");
         add("2");
         add("3");
     }
}

which, one comapcted, gives

collection = new ArrayList<String>() {{ add("1"); add("2"); add("3"); }}

FUGLY, to say the least. However, there is a variant to the Arrays.asList method : Arrays.asList(T...a) which provides comapcity and readability. As an example, it gives the following line of code :

collection = new ArrayList<String>(Arrays.asList("1", "2", "3")); // yep, this one is the shorter

And notice you don't create an anonymous subclass of ArrayList of dubious use.

Textualist answered 22/9, 2010 at 13:6 Comment(0)
D
5

Maybe that was

Collection<String> collection = new ArrayList<String>() {{
    add("foo");
    add("bar");
}};

Also known as double-bracket initialization.

Dobbin answered 22/9, 2010 at 13:5 Comment(0)
O
5

From JDK9 you can use factory methods:

List<String> list = List.of("foo", "bar", "baz");
Set<String> set = Set.of("foo", "bar", "baz");
Outclass answered 7/6, 2022 at 12:1 Comment(1)
These are great methods but cannot be used for mutable collections.Bide
N
3

You could create an utility function:

@SafeVarargs
public static <T> List<T> listOf(T ... values) {
    return new ArrayList<T>(Arrays.asList(values));
}

So you could call it like:

collection = MyUtils.listOf("1", "2", "3");

That way, you can populate a list very easily, and still keep it mutable.

Numberless answered 5/7, 2016 at 14:12 Comment(0)
C
0

See Initialization of an ArrayList in one line

Cosby answered 22/9, 2010 at 13:6 Comment(0)
B
0

Maybe it's just me but I dont see the point of complicating things, purely in pursuit of writing shorter/faster code. Given the choice of typing marginally fewer lines of code and far easier debugging/revising I am pretty sure I'd choose the second option.

So unless there is a particular reason to push for keeping this short, I'd say stick to Ockham's razor, and given a number of solutions to a problem, go with the simplest one. That way when something does go wrong (which Murphy's law clearly dictates it will) you'll have a much easier time to track it. :)

Berhley answered 22/9, 2010 at 13:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.