java - 添加到 Map 时如何将 double 组装箱为 double 组?

标签 java hashmap boxing

我要填写HashMap<Integer,Double[]>

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

只是常规intdouble[] ,最好的方法是什么?

我明白了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/

相关文章:

java - EJB 中的资源注入(inject)问题

java - 同步线程池ExecutorService

java - 处理大型 HashMap 的内存高效方式

java - 如何从 List<Integer> 中获取 IntStream?

rust - 如何在 Rust 中创建盒装闭包向量?

java - f :ajax does not render target components in jsf app

java - 什么时候调用 java 的 thread.run() 而不是 thread.start()?

javascript - 如何正确地将 Java Hashmap 转换为 JavaScript 数组

java - 如何在android中提取hashmap的arraylist列表并循环遍历它?

c# - 盒装值类型比较