如何在 Java/Android 中的 Hashmap 中“打印”一个 HashMap

我想Hashmap<String, Integer>在Hashmap<String, Hashmap<String, Integer>>(Beta) 中“打印”一个(比如说 Alpha),我说“打印”是因为我不希望“打印”的 Alpha 在我重新使用 Alpha 时发生变化。


例如:


class scratch_2 {

    public static void main(String[] args) {

        HashMap<String, Integer> Alpha = new HashMap<>();

        HashMap<String, HashMap<String, Integer>> Beta = new HashMap<>();

        Beta.put("A1", Alpha);

        Beta.put("B2", Alpha);

        Alpha.put("A", 1);

        Alpha.put("B", 2);


        System.out.println(Beta); --->print1


        Alpha.clear();


        System.out.println(Beta); ---->print2

    }

}

打印1的结果:{A1={A=1, B=2}, B2={A=1, B=2}}

打印2的结果:{A1={}, B2={}}


如何设置 Beta.put() 以便在清除 Alpha 时,Beta 保持不变?


白衣非少年
浏览 179回答 2
2回答

慕标5832272

以下是重置嵌套 HashMap 的方法。该值在第 22 行被删除,然后作为新的内部 hashmap 实例添加回来。再次,我遍历 containsMap 和 innerMap 获取每个地图。一旦我有一个要重置的值,我就会调用重置函数。import java.util.*;import java.lang.*;public class Collections {public HashMap<String, HashMap<String, Integer>>&nbsp;&nbsp;createMap(String beta, String alpha, int m, HashMap<String,&nbsp;&nbsp;Integer> innerStructure, HashMap<String, HashMap<String,&nbsp;&nbsp; Integer>> containingStructure) {&nbsp; &nbsp; while(m>0) {&nbsp; &nbsp; &nbsp; &nbsp; innerStructure.put(beta, m);&nbsp; &nbsp; &nbsp; &nbsp; m--;&nbsp; &nbsp; }&nbsp; &nbsp; containingStructure.put(alpha, innerStructure);&nbsp;&nbsp; &nbsp; return containingStructure;}public void reset(HashMap<String, HashMap<String, Integer>>&nbsp;map, int x) {&nbsp; &nbsp; HashMap<String, Integer> betaMap = new HashMap<String, Integer>();&nbsp; &nbsp; for(Map.Entry<String,HashMap<String,Integer>> entry: map.entrySet()) {&nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Key before is:" + entry.getKey());&nbsp; &nbsp; &nbsp; &nbsp; if(entry.getValue() instanceof Map) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for(Map.Entry<String, Integer> mapEntry: entry.getValue().entrySet()) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(mapEntry.getValue() == x) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; entry.getValue().remove(x);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; map.put(entry.getKey(), betaMap);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp;}&nbsp;public void print(HashMap<String, HashMap<String, Integer>> map) {&nbsp; &nbsp; for(Map.Entry<String,HashMap<String,Integer>> entry: map.entrySet()) {&nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Key is:" + entry.getKey());&nbsp; &nbsp; &nbsp; &nbsp; if(entry.getValue() instanceof Map) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for(Map.Entry<String, Integer> mapEntry: entry.getValue().entrySet()) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println(mapEntry.getKey());&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}public static void main(String[] args) {&nbsp; &nbsp; Collections collections = new Collections();&nbsp; &nbsp; HashMap<String, Integer> innerStructure = new HashMap<>();&nbsp; &nbsp; HashMap<String, HashMap<String, Integer>> containingStructure = new HashMap<>();&nbsp; &nbsp; containingStructure = collections.createMap("B1", "A1", 4, innerStructure, containingStructure);&nbsp; &nbsp; collections.reset(containingStructure, 2);&nbsp; &nbsp; collections.print(containingStructure);}}它真的不会改变测试版,而是评估每个条目并确保它是一个新的 HashMap 类型。我认为这应该有所帮助。

红颜莎娜

我应该Alpha = new HashMap<>();在我不再需要它时通过分配它来创建一个新的 Alpha 实例。然后我可以重新使用它而不会影响 Beta。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python