Can someone please provide an example of creating a Java ArrayList
and HashMap
on the fly? So instead of doing an add()
or put()
Use a nice anonymous initializer:
List list = new ArrayList() {{
add("a");
add("b");
}};
Same goes for a Map:
Map map = new HashMap() {{
put("a", "a");
put("b", "b");
}};
I find this the most elegant and readable.
Other methods demand creating an array first, then converting it to a List - too expensive in my taste, and less readable.