我要填写HashMap<Integer,Double[]>
Map<Integer,Double[]> cached_weights = new HashMap<Integer,Double[]>();
只是常规int
和double[]
,最好的方法是什么?
我明白了this question ,但它回答了相反的问题。
最佳答案
对于键(Integer),编译器会自动为您处理,您可以直接传递 int 值。
对于 boolean 数组,您可以使用 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));
DoubleStream 的装箱
方法返回一个由该流的元素组成的 Stream,装箱为 Double。
然后您将获得一个 Stream,您可以在其中轻松调用 toArray
将其转换为 Double[]
。
关于java - 添加到 Map 时如何将 double 组装箱为 double 组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28687804/