8

I know I can declare and initialize a List using double braces:

// (1)
List<Object> myList = new ArrayList<object>(){{
    add("Object1");
    add("Object2");
}};

But I want a List of <Map<Object,Object>>:

// (2)
List<Map<Object,Object>> myList = new ArrayList<Map<Object,Object>>();

How can I use double brace initialization (see (1)) with nested collections? My goal is to declare and initialize the data structure in a single line.

Also I would like to know if there are certain drawbacks when using double brace initialization I have to be aware of.

5
  • 2
    Just use "normal" initialization code; double brace initialization is difficult to read. Code is WORM (Write Once, Read Many). Note: if this is a static list, use a static initializer. Commented Feb 9, 2015 at 12:58
  • 1
    Why would you want to write code like that? Do you work alone? Commented Feb 9, 2015 at 13:00
  • 4
    Don't use the "double braces", because each time you do that, you are creating an useless subclass of ArrayList. Commented Feb 9, 2015 at 13:00
  • @kolossus: why, can't i add this in my coding standard? Commented Feb 9, 2015 at 13:03
  • 1
    possible duplicate of What is Double Brace initialization in Java? Commented Feb 9, 2015 at 13:14

2 Answers 2

10

Avoid double brace initialization as it a) surprises your colleagues and is hard to read, b) harms performance and c) may cause problems with object equality (each object created has a unique class object).

If you're working on a code style guide this kind of trickery belongs in the don't section.

If you really want to be able to create lists and maps inline, just go ahead and create a few factory methods. Could look like this:

List l = Lists.of(
    Maps.of(new Entry("foo", "bar"), new Entry("bar", "baz")),
    Maps.of(new Entry("baz", "foobar"))
);

However, Vangs example shows exactly how the syntax for your example is for double brace initialization.

Sign up to request clarification or add additional context in comments.

1 Comment

Double brace code can also throw up issues in testing. How would you mock xyz if it is statically initialized, for example?
5

It's not good idea, because it's hard to read, but you can do following:

List<Map<Object,Object>> myList = new ArrayList<Map<Object,Object>>() {{
                    add(new HashMap<Object, Object>() {{
                        put(key, obj);
                    }});
                }};

5 Comments

@vang: thanks for your time and answer but in this you didn't initlize the list :(
What do you mean? Than what new ArrayList<Map<Object,Object>>() is?
@Vang: i meant that you gave me an idea to put objects in map..it's fine but want to init. my list as well. hope you got it :)
Vangs example is correct and creates a List<Map<Object, Object>> with exactly one map having exactly one entry of key: object.
I'm afraid we don't understand each other) By the way it better to accept @atamanroman answer

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.