繁体   English   中英

如何使用流从另一个 HashMap 填充一个 HashMap

[英]How to fill a HashMap from another HashMap using streams

我正在尝试从填充的 HashMap<String,Product> 应用 Java Streams 创建一个新的 Hashmap<Integer, Map<String, Product>> 并且我一直收到java.util.HashMap$Node cannot be cast to class java。 util.Map ,我看不出有什么问题。

可能有一种更简单、更合乎逻辑的方法,但这就是我想出的


Map<String, Product> hashMapChild = new HashMap<>();

hashMapChild.put("PC000123", new Product("reference1", "product1", price1));
hashMapChild.put("PC000234", new Product("reference2", "product2", price2));


Map<Integer, Map<String, Product>> hashMapParent = hashMapChild.entrySet()
                .stream()
                .collect(HashMap<Integer, Map<String, Product>>::new,
                         (map, streamValue) -> map.put((int) Math.floor(Math.random()*100),(Map<String, Product>) streamValue),
                                    
                         (map, map2) -> {
                             System.out.println(map);
                             System.out.println(map2);
                         });
        
        hashMapParent.forEach((k, v) -> System.out.println(k + ":" + v));

另一方面。 如何获得自动增量索引而不是随机值?

提前谢谢了

output 可能是这样的:

<1, <COMPGAM012, Product{reference='COMPGAM012', name='laptop', prize=750.56}>>
<2, <PC000124, Product{reference='PC000124', name='Desktop', prize=400.56}>>

根据您提供的代码,hashMapChild 包含 2 个数据,并且根据为 hashMapParent 定义的结构,它应该将整个 hashMapChild 作为 hashMapParent 的值。 例如:

Map<String, Product> hashMapChild = new HashMap<>();
hashMapChild.put("PC000123", new Product("reference1", "product1", price1));
hashMapChild.put("PC000234", new Product("reference2", "product2", price2));

Map<Integer, Map<String, Product>> hashMapParent = new HashMap<>();
hashMapParent.put(0, hashMapChild);

从代码看来,您将只有一个包含所有产品的hashMapChild ,所以我认为在这种情况下您不需要进行任何类型的迭代。

或者我是否应该考虑添加更多像这样的Map<String, Product> hashMapChild = new HashMap<>(); 并将它们添加到hashMapParent中,例如:

hashMapParent.put(0, hashMapChild);
hashMapParent.put(1, hashMapChild1);
hashMapParent.put(2, hashMapChild2);

我的理解是,您想为 Map 中的每个条目分配一个索引。因此,您想要的结果不是Map<Integer, Map<String, Product>> 实际上你想要Map<Integer, Map.Entry<String, Product>>

对于索引,您可以使用AtomicInteger

AtomicInteger index = new AtomicInteger(1);

Map<Integer, Map.Entry<String, Product>> hashMapParent = hashMapChild.entrySet()
                .stream()
                .collect(HashMap<Integer, Map.Entry<String, Product>>::new,
                        (map, streamValue) -> map.put(index.getAndIncrement(), streamValue),

                        (map, map2) -> {
                            System.out.println(map);
                            System.out.println(map2);
                        });

简化版:

Map<Integer, Map.Entry<String, String>> hashMapParent = hashMapChild.entrySet()
                .stream()
                .collect(Collectors.toMap(
                        k -> index.getAndIncrement(),
                        v -> v
                ));

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM