Is there a more streamlined way to do the following?
Map<String, String> map = new HashMap<String, String>();
map.put("a", "apple");
map.put("b", "bear");
map.put("c", "cat");
I'm looking for something closer to this.
Map<String, String> map = MapBuilder.build("a", "apple", "b", "bear", "c", "cat");
There's always double-brace initialization:
Map<String, String> map = new HashMap<String, String>(){{
put("a", "apple"); put("b", "bear"); put("c", "cat");}};
There are problems with this approach. It returns an anonymous inner class extending HashMap, not a HashMap. If you need to serialize the map then know that serialization of inner classes is discouraged.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With