Is there a way in java to specify the elements in a Map when calling the Map's constructor?
As an example of what I mean, let's say I want a map of the following key/value pairs:
foo: bar
baz: bat
I can add them to an existing Map like this:
Map<String, String> myMap = new HashMap<String, String>();
myMap.put("foo", "bar");
myMap.put("baz", "bat");
Is there a way to do something like this (with object literals borrowed from javascript as a substitute for what I want):
Map<String, String> myMap = new HashMap<String, String>({"foo": "bar", "baz": "bat"});
The reason I ask (in case it makes a difference), is that I want to pass a bunch of HashMaps in-line to a function call, and it would be quite convenient if I didn't have to create variables to hold them and add elements to each one, one at a time. I'm considering using a JSON parser and writing my Maps as javascript objects to save time, but it would a bit silly if that was really the optimal way to do this.