java - Java 1.8中HashMap的HashMap检索

标签 java hashmap

对于下面的代码,在打印出HashMap temp的HashMap时,为什么会打印

{0={PRICE=2.2, NAME=2str}, 1={PRICE=2.2, NAME=2str}}

作为输出而不是获取 {0={PRICE=2, NAME=1str}, 1={PRICE=2.2, NAME=2str}}

请告知是否有不同的基本概念用于创建此类 HashMap ?

我试过在 test.put(0,temp); 之后插入 temp.clear(); 但结果还是一样。

import java.util.*;

public class TestClass {

    public static void main(String args[]){
        HashMap<Integer,HashMap<String, Object>> test = new HashMap<Integer,HashMap<String, Object>>();
        HashMap<String,Object> temp = new HashMap<String, Object>();

        temp.put("NAME","1str");
        temp.put("PRICE", 2);

        test.put(0,temp);
        System.out.println(temp);

        temp.put("NAME","2str");
        temp.put("PRICE",2.2);
        test.put(1,temp);

        System.out.println(temp);
        System.out.println(test);
    }

}

最佳答案

您正在使用 map 的相同实例,因此请考虑键发生了什么。

关于java - Java 1.8中HashMap的HashMap检索,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53611232/

相关文章:

java - Android 数据绑定(bind)将参数传递给 onClick 方法

java - 如何在 Lucene 分析器上使用多个过滤器 (Lucene 4.4)

java - map 中的集合 : Undefined k, v

c++ - 多个线程在 C++ 中访问 std::map

android - 如何使用 ArrayList<HashMap<String, String>> 中的插入数据到 Android Studio 中的 ListView

java - Bndtools:准备运行时环境时出现问题

java - BufferedWriter 类的 writeLine 方法

Javascript - 按每个数组中元素的升序重新排列数组元素

java - PrimeFaces 3 TabView 带有额外的空白

java - HashMap<类<?>, 列表<类<?>>> : specify that the lists' classes extend the keys'