簡體   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