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?

1
  • 2
    Should be new String[]{ array elements} Commented Oct 4, 2012 at 17:47

3 Answers 3

12

This will do it:

instruments.put("EURUSD", new String[]{"4001","EURUSD","10000","0.00001","0.1","USD"});
Sign up to request clarification or add additional context in comments.

2 Comments

can you add to that array there?
@S.Matthew_English Just add a new string at the end. Maybe I don't understand what you're asking?
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"});
     }
 };

5 Comments

Otherwise known as an instance initializer block.
It's also subclassing the HashMap.
@BheshGurung. Yup thats true.. But since OP wanted it in one step.. So I gave him.. :) :)
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 :)
@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.
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"}).

3 Comments

but how to add to that array once you put it there?
You can get the array with the hash map. But you can't add to the if thats what you mean.
i did the thing that amit on the bottom recommended here do you think that's a good solution? do you know how to print something like that by any chance?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.