I am trying to insert static data into a HashMap in Java like this:
HashMap<String,String[]> instruments = new HashMap<String, String[]>();
instruments.put("EURUSD", {"4001","EURUSD","10000","0.00001","0.1","USD"});
But the compiler doesn't like it. The only way I found to insert that data into the HashMap is to declare the string array separately and then put it into the HashMap, like this
String[] instruDetails = {"4001","EURUSD","10000","0.00001","0.1","USD"};
instruments.put("EURUSD", instruDetails);
But it not very expressive, and hard to maintain
So my question is, is there a way to do the put()
operation and string array declaration in one step/line?
This will do it:
instruments.put("EURUSD", new String[]{"4001","EURUSD","10000","0.00001","0.1","USD"});
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