5

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?

Baz
  • 36,440
  • 11
  • 68
  • 94
jule64
  • 487
  • 1
  • 6
  • 19

3 Answers3

12

This will do it:

instruments.put("EURUSD", new String[]{"4001","EURUSD","10000","0.00001","0.1","USD"});
Baz
  • 36,440
  • 11
  • 68
  • 94
8

To get it all in one sentence, use double-braces initialization: -

 HashMap<String,String[]> instruments = new HashMap<String, String[]>() {
     {
      put("EURUSD", new String[]{"4001","EURUSD","10000","0.00001","0.1","USD"});
      put("EUR", new String[]{"4001","EURUSD","10000","0.00001","0.1","USD"});
     }
 };
Rohit Jain
  • 209,639
  • 45
  • 409
  • 525
  • Otherwise known as an [_instance initializer block_](http://docs.oracle.com/javase/tutorial/java/javaOO/initial.html). – Ted Hopp Oct 04 '12 at 17:52
  • It's also subclassing the `HashMap`. – Bhesh Gurung Oct 04 '12 at 17:52
  • @BheshGurung. Yup thats true.. But since OP wanted it in one step.. So I gave him.. :) :) – Rohit Jain Oct 04 '12 at 17:53
  • I think the actual question is why the `put` method call doesn't work. But still you introduced the OP with something new, I think. +1 :) – Bhesh Gurung Oct 04 '12 at 17:57
  • @RohitJain Thanks this is useful. I have seen this construct with Listeners before but did not realise subclassing worked with HashMap too. Will look to use this construct more often. – jule64 Oct 04 '12 at 18:45
6

I think you already got what works. But the reason that

instruments.put("EURUSD", {"4001","EURUSD","10000","0.00001","0.1","USD"});

doesn't work is because {"4001","EURUSD","10000","0.00001","0.1","USD"}. {} is a syntactic sugar or short-cut in Java array for initialization. It comes with a constraint that it always has to go along with the array declaration statement, otherwise it's a syntax error.

Array declaration statement like

String[] array = {"1", "2"};

That way Java knows that the array that it needs to create for you is actually of String type elements.

If you break the above statement as follows

String[] array;
array = {"1", "2"};

It doesn't compile.

And with the new String[]{"4001","EURUSD","10000","0.00001","0.1","USD"}, the compiler knows that it has to instantiate a new array which element type is String (new String[]) and initialize the newly instantiated array with values you provided ({"4001","EURUSD","10000","0.00001","0.1","USD"}).

Bhesh Gurung
  • 50,430
  • 22
  • 93
  • 142
  • but how to add to that array once you put it there? – smatthewenglish Apr 23 '15 at 03:13
  • You can get the array with the hash map. But you can't add to the if thats what you mean. – Bhesh Gurung Apr 23 '15 at 03:19
  • i did the thing that amit on the bottom recommended [here](http://stackoverflow.com/questions/13095076/hashmapstring-arraylist-appending-arraylist-with-new-values-based-on-the-key) do you think that's a good solution? do you know how to print something like that by any chance? – smatthewenglish Apr 23 '15 at 03:23