Java Intelij idea哈希映射打印到控制台

Java Intelij idea哈希映射打印到控制台,java,intellij-idea,hashmap,console-application,Java,Intellij Idea,Hashmap,Console Application,我试图将数据从2个hashmaps打印到控制台,但我注意到这里只描述了一个。下面是一个代码示例: public class HashMapsmathces { public static void main(String[] args) throws Exception { Map<String, Object> hm1 = new HashMap<>(); hm1.put("id", 1); hm1.put("sku","qazwsx

我试图将数据从2个hashmaps打印到控制台,但我注意到这里只描述了一个。下面是一个代码示例:

public class HashMapsmathces {
    public static void main(String[] args) throws Exception {

    Map<String, Object> hm1 = new HashMap<>();

    hm1.put("id", 1);
    hm1.put("sku","qazwsx");
    hm1.put("price", 11);

    printMaps(hm1);

    Map<String, Object> hm2 = new HashMap<>();

    hm1.put("id", 2);
    hm1.put("sku","qazwsx");
    hm1.put("price", 13);

    printMaps(hm2);
}

public static void printMaps(Map<String, Object> map)
{

    for (Map.Entry<String, Object> pair : map.entrySet())
    {
        String key = pair.getKey();
        String value = pair.getValue().toString();
        System.out.println(key + " : " + value);
    }
    }
}
公共类HashMapsmathces{
公共静态void main(字符串[]args)引发异常{
Map hm1=新的HashMap();
hm1.put(“id”,1);
hm1.put(“sku”、“qazwsx”);
hm1.卖出价(“价格”,11);
印刷地图(hm1);
Map hm2=新的HashMap();
hm1.put(“id”,2);
hm1.put(“sku”、“qazwsx”);
hm1.卖出价(“价格”,13);
印刷地图(hm2);
}
公共静态void打印地图(地图地图)
{
for(Map.Entry对:Map.entrySet())
{
String key=pair.getKey();
字符串值=pair.getValue().toString();
System.out.println(键+“:”+值);
}
}
}
所以,当我按下“Run”时,只有
hm1
会被推到控制台。我不太清楚为什么

这里还有一个截图


谢谢。

这是因为hm2是空的,所以在声明hm2映射后,您要将值重新分配给hm1

public class HashMapsmathces {
    public static void main(String[] args) throws Exception {

    Map<String, Object> hm1 = new HashMap<>();

    hm1.put("id", 1);
    hm1.put("sku","qazwsx");
    hm1.put("price", 11);

    printMaps(hm1);

    Map<String, Object> hm2 = new HashMap<>();

    hm2.put("id", 2);
    hm2.put("sku","qazwsx");
    hm2.put("price", 13);

    printMaps(hm2);
}

public static void printMaps(Map<String, Object> map)
{
    //напишите тут ваш код
    for (Map.Entry<String, Object> pair : map.entrySet())
    {
        String key = pair.getKey();
        String value = pair.getValue().toString();
        System.out.println(key + " : " + value);
    }
    }
}
公共类HashMapsmathces{
公共静态void main(字符串[]args)引发异常{
Map hm1=新的HashMap();
hm1.put(“id”,1);
hm1.put(“sku”、“qazwsx”);
hm1.卖出价(“价格”,11);
印刷地图(hm1);
Map hm2=新的HashMap();
hm2.put(“id”,2);
hm2.put(“sku”、“qazwsx”);
hm2.卖出价(“价格”,13);
印刷地图(hm2);
}
公共静态void打印地图(地图地图)
{
//напишите тут ваш код
for(Map.Entry对:Map.entrySet())
{
String key=pair.getKey();
字符串值=pair.getValue().toString();
System.out.println(键+“:”+值);
}
}
}

这是因为hm2是空的,所以在声明hm2映射后,您要将值重新分配给hm1

public class HashMapsmathces {
    public static void main(String[] args) throws Exception {

    Map<String, Object> hm1 = new HashMap<>();

    hm1.put("id", 1);
    hm1.put("sku","qazwsx");
    hm1.put("price", 11);

    printMaps(hm1);

    Map<String, Object> hm2 = new HashMap<>();

    hm2.put("id", 2);
    hm2.put("sku","qazwsx");
    hm2.put("price", 13);

    printMaps(hm2);
}

public static void printMaps(Map<String, Object> map)
{
    //напишите тут ваш код
    for (Map.Entry<String, Object> pair : map.entrySet())
    {
        String key = pair.getKey();
        String value = pair.getValue().toString();
        System.out.println(key + " : " + value);
    }
    }
}
公共类HashMapsmathces{
公共静态void main(字符串[]args)引发异常{
Map hm1=新的HashMap();
hm1.put(“id”,1);
hm1.put(“sku”、“qazwsx”);
hm1.卖出价(“价格”,11);
印刷地图(hm1);
Map hm2=新的HashMap();
hm2.put(“id”,2);
hm2.put(“sku”、“qazwsx”);
hm2.卖出价(“价格”,13);
印刷地图(hm2);
}
公共静态void打印地图(地图地图)
{
//напишите тут ваш код
for(Map.Entry对:Map.entrySet())
{
String key=pair.getKey();
字符串值=pair.getValue().toString();
System.out.println(键+“:”+值);
}
}
}

您确实正在打印这两张地图。但是
hm2
为空。(您只需添加到
hm1

您确实在打印这两个地图。但是
hm2
为空。(您只添加到
hm1

乔,谢谢。我没有注意到那个打字错误。在问这样的问题之前,我可能应该仔细检查代码。在出现堆栈溢出之前,花一些时间在调试器中也会很有用。Joe,谢谢。我没有注意到那个打字错误。在问这样的问题之前,我可能应该仔细检查一段代码。在出现堆栈溢出之前,在调试器中花费一些时间也会很有用。这是因为hm2是空的,所以在声明hm2映射之后,您要将值重新分配给hm1。Pfb答案是因为hm2是空的,所以在声明hm2映射后,您正在将值重新分配给hm1。答案是什么