2

I want to fill up a HashMap<Integer,Double[]>

Map<Integer,Double[]> cached_weights = new HashMap<Integer,Double[]>();

with just regular int and double[], what's the best way to do that?

I see this question, but it answers the opposite question.

Community
  • 1
  • 1
smatthewenglish
  • 2,831
  • 4
  • 36
  • 72

1 Answers1

2

For the key (Integer) the compiler will handle this automatically for you and you can pass directly an int value.

For the Boolean array, you could handle this way with Java 8

Map<Integer, Double[]> foo = new HashMap<Integer, Double[]>();
double[] bar = new double[10];
//As you can see, 1 is passed directly and will be converted to Integer object.
foo.put(1, Arrays.stream(bar)
            .boxed()
            .toArray(Double[]::new));

the boxed method of DoubleStream returns a Stream consisting of the elements of this stream, boxed to Double.

Then you get a Stream on which you can easily call toArray to convert to a Double[].

Jean-François Savard
  • 20,626
  • 7
  • 49
  • 76