Single-line Collection Creation

09 October 2007 ~ blog java

I like having nice compact code. No, I am not one of those "write the whole app on one line" developers, but I do
like code collapsed and out of the way. One of the things that has always annoyed me was that while you can create and
populate an array on one line, you cannot do the same with Maps, Lists, and Sets: but, I finally realized that
there is a very simple way to do it using instance initializers.

Map<String,String> map = new HashMap<String, String>(){{ put("akey","avalue"); }}

Basically you are anonymously extending the HashMap and calling the put() method to populate the data. Notice the
double curley braces, which signify instance initialization.

I am not suggesting that all of your collection populating should be done this way; however, it is nice when you simply
want a single value put in a map for some reason.


Creative Commons License CoffeaElectronica.com content is copyright © 2016 Christopher J. Stehno and available under a Creative Commons Attribution-ShareAlike 4.0 International License.