Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Declare and Put String array in HashMap in one step

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?

like image 319
jule64 Avatar asked Oct 04 '12 17:10

jule64


1 Answers

This will do it:

instruments.put("EURUSD", new String[]{"4001","EURUSD","10000","0.00001","0.1","USD"});
like image 52
Baz Avatar answered Oct 06 '22 01:10

Baz