These are equivalent code segments:
Hacky syntax:
Map map = new HashMap() {{
put("key", "value");
}};
Normal syntax:
Map map = new HashMap();
map.put("key", "value");
Usages
This hacky syntax could be used gracefully when initializing a class member. This way you will avoid adding code to constructor(s) or in static initializer of the class.
No comments:
Post a Comment