Really simple question hopefully. I want to do something like this:
Map<String, String> temp = { colName, data };
Where colName
, data
are string variables.
Thanks.
The Static Initializer for a Static HashMap We can also initialize the map using the double-brace syntax: Map<String, String> doubleBraceMap = new HashMap<String, String>() {{ put("key1", "value1"); put("key2", "value2"); }};
A Map is an object that maps keys to values. A map cannot contain duplicate keys: Each key can map to at most one value. It models the mathematical function abstraction.
Map is an interface. Create an instance of one the classes that implements it:
Map<String, String> temp = new HashMap<String, String>();
temp.put(colName, data);
Or, in Java 7:
Map<String, String> temp = new HashMap<>();
temp.put(colName, data);
@JohnGirata is correct.
If you're REALLY upset, you could have a look here http://nileshbansal.blogspot.com.au/2009/04/initializing-java-maps-inline.html
It's not quite what you're asking, but is a neat trick/hack non the less.
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