1
votes

I have the following HashMap:-

HashMap<Integer,Integer[]> possibleSeq = new HashMap<Integer,Integer[] >();

I would like to add into the map something like this:-

 possibleSeq.put(1,{1,2,3,4});

There are a large number of entries and i am supposed to enter manually:- I tried doing this:-

Integer a = 1;
Integer aArr = {1,2,3,4};
  possibleSeq.put(a,aArr);

But this is not my requirement.I dont want to create separate Integer variables to store keys and separate Integer Arrays to store my values ie IntegerArray .Any Ideas??

4
Why Integer[]? I'm pretty sure you need int[]. - Marko Topolnik
Generics don't work on primitives I think - arynaq
int[] is an array, which is an object, so it's OK. Just tried it, works fine. - ajb

4 Answers

7
votes

How about this:

public static void put(Map<Integer, Integer[]> map, Integer k, Integer... v) {
    map.put(k, v);
}

...

put(map, 1, 1,2,3,4);
1
votes

You can new the Integer[] inline:

possibleSeq.put(1, new Integer[]{1,2,3,4});
0
votes
possibleSeq.put(1,{1,2,3,4});

This is not valid Java syntax. Try this instead:

possibleSeq.put(1, new Integer[]{1,2,3,4});
0
votes

{1,2,3,4,5,6} is not an array new Integer[]{1,2,3,4,5} is an array of integers.

 possibleSeq.put(1,new Integer[]{1,2,3,4});