For most Java programmers, this will be nothing new but as I did not grew up with this language (fortunately!), I did not know about this way to initialize a collection with a few members in one line using anonymous classes and initializer blocks.

I did however knew about using Arrays.asList() to create a list in place and of course with varargs you can write such little helpers for every kind of collection but for maps the approach above seems superior. I also think that this is something to use mainly in testing code but sometimes it can even come in handy in production code.

Advertisements