简体   繁体   English

HashMap-不获取异常输出

[英]HashMap - Doesnot get excepted output

I have following 2 cases: 我有以下2种情况:

case 1: 情况1:

    HashMap<String, HashMap<String,String>> mainMap  = new HashMap<String, HashMap<String,String>>();

    HashMap <String, String> subMap  = new HashMap<String,String>();

    subMap.put("11", "12");
    subMap.put("13", "124");
    subMap.put("21", "122");
    subMap.put("14", "152");

    System.out.println("For One : "+subMap);
    mainMap.put("one", subMap);

    subMap.put("15", "152");
    subMap.put("17", "152");

    System.out.println("For Two : "+subMap);
    mainMap.put("two", subMap);

    System.out.println(mainMap); 

I am excepting the output as: 我除了输出为:

For One : {21=122, 13=124, 14=152, 11=12}
For Two : {21=122, 17=152, 15=152, 13=124, 14=152, 11=12}
{two={21=122, 17=152, 15=152, 13=124, 14=152, 11=12}, one={21=122, 13=124, 14=152, 11=12}}

But I got the output as : 但是我得到的输出为:

For One : {21=122, 13=124, 14=152, 11=12}
For Two : {21=122, 17=152, 15=152, 13=124, 14=152, 11=12}
{two={21=122, 17=152, 15=152, 13=124, 14=152, 11=12}, one={21=122, 17=152, 15=152, 13=124, 14=152, 11=12}}

WHY ? 为什么呢?

case 2: 情况2:

I also tried with : 我也尝试过:

HashMap<String,String> map12 = new HashMap<String, String>();

map12.put("1","1234");
map12.put("2","1234");

System.out.println("map12 : "+map12);

map12.put("3","1234");

and I got output as i excepted as: 和我得到的输出,我除外:

map12 : {2=1234, 1=1234}

Can Explain please why in case1 i dont get output as i excepted? 可以解释一下为什么在case1我没有得到我所排除的输出吗?

You are using the same hashmap, you have to create a new instance of it. 您使用的是相同的哈希图,因此必须创建它的新实例。

HashMap<String, HashMap<String,String>> mainMap  = new HashMap<String, HashMap<String,String>>();

HashMap <String, String> subMap  = new HashMap<String,String>();

subMap.put("11", "12");
subMap.put("13", "124");
subMap.put("21", "122");
subMap.put("14", "152");

System.out.println("For One : "+subMap);
mainMap.put("one", subMap);

subMap  = new HashMap<String,String>(); /// <<<<<<<<
subMap.put("21", "122");
subMap.put("17", "152");
subMap.put("15", "152");
subMap.put("13", "124");
subMap.put("14", "152");
subMap.put("11", "12");

System.out.println("For Two : "+subMap);
mainMap.put("two", subMap);

System.out.println(mainMap); 

The value in your mainMap is a reference to a HashMap object. mainMap中的值是对HashMap对象的引用 Any reference to that object (eg subMap ) can change it. 对该对象的任何引用(例如subMap )都可以更改它。

If you are confused, this may help: 如果您感到困惑,这可能会有所帮助:

This line will create a HashMap object, and assigned a subMap reference to it. 该行将创建一个HashMap对象,并为其分配一个subMap引用。 HashMap subMap = new HashMap(); HashMap subMap = new HashMap();

subMap-------->[hashObject] 子图--------> [hashObject]

This line will add "one" as key, and subMap "reference" as value mainMap.put("one", subMap); 该行将添加“ one”作为键,并将subMap“ reference”作为值mainMap.put(“ one”,subMap);

so, mainMap.put("one", subMap---------------> [hashObject] 因此,mainMap.put(“ one”,subMap --------------------- [hashObject]

Then you prints the value assign to key "one" ie {21=122, 13=124, 14=152, 11=12} 然后打印分配给键“ one”的值,即{21 = 122,13 = 124,14 = 152,11 = 12}

Now, you are putting values to the same HashMap object [hashObject] 现在,您将值放入相同的HashMap对象[hashObject]

subMap.put("15", "152");

subMap.put("17", "152");

Now, map contains 现在,地图包含

{21=122, 17=152, 15=152, 13=124, 14=152, 11=12}

And then, you assigned it as: 然后,您将其分配为:

mainMap.put("two", subMap---------------> [hashObject]

Try this: 尝试这个:

System.out.println("For One : "+subMap);    

System.out.println("For Two : "+subMap);

Output: 输出:

For One : {11=12, 13=124, 14=152, 15=152, 17=152, 21=122}

For Two : {11=12, 13=124, 14=152, 15=152, 17=152, 21=122}

which is the same HashMap object, no new object is created and both "one" and "two" has value, a reference referring to the same object. 这是相同的HashMap对象,不会创建新对象,并且“一个”和“两个”都具有值,该引用引用了同一对象。

Please note that this is an object not a primitive. 请注意,这不是原始对象。

int a = 10;
int b = a;
a++;

output: 输出:

a=11

b=10

but in case of object its reference not the object, 但如果是对象,则其引用不是对象,

Object a = new Object();

Object b = a;

any change done using any of these references will effect the same object, because both are referring the same object. 使用这些引用中的任何一个所做的任何更改都会影响同一个对象,因为两者都引用同一个对象。

In your case 2, you have first print the map and then added the next key-value. 在您的情况2中,您首先打印了地图,然后添加了下一个键值。

HashMap doesn't guarantee a specific order of the items stored inside. HashMap不保证存储在其中的项目的特定顺序。 It's unordered. 它是无序的。

Use LinkedHashMap instead. 请改用LinkedHashMap It stores the items in the order, in which they were inserted. 它按插入顺序存储项目。

Read the Java Docs for more Infos: 阅读Java文档以获取更多信息:

http://docs.oracle.com/javase/6/docs/api/java/util/HashMap.html http://docs.oracle.com/javase/6/docs/api/java/util/LinkedHashMap.html http://docs.oracle.com/javase/6/docs/api/java/util/HashMap.html http://docs.oracle.com/javase/6/docs/api/java/util/LinkedHashMap.html

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

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