簡體   English   中英

從Java 1.8中的HashMap的HashMap檢索

[英]Retrieving from HashMap of HashMap in Java 1.8

對於以下代碼,在打印出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}}嗎?

請告知是否存在用於創建此類哈希圖的其他基本概念?

我試過插入temp.clear(); test.put(0,temp); 但結果保持不變。

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);
    }

}

您使用的是地圖的同一實例 ,因此請考慮密鑰發生了什么。

為temp創建一個新實例,不要重用舊實例...
像這樣:

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 = new HashMap<String, Object>();
    temp.put("NAME","2str");
    temp.put("PRICE",2.2);
    test.put(1,temp);

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

使用另一個HashMap實例,可以解決問題

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);

    HashMap<String, Object> temp1 = new HashMap<String, Object>();
    temp1.put("NAME", "2str");
    temp1.put("PRICE", 2.2);
    test.put(1, temp1);

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

您沒有得到預期的輸出,因為當您這樣做時

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

您正在替換鍵NAME的先前值。 當你做同樣的事情

temp.put("PRICE",2.2);

為了避免這種情況,您可以創建一個新的HashMap並防止替換同一鍵的值

這是一個學校問題:java是按值傳遞(傳遞對象的副本)還是按引用傳遞(傳遞相同的對象)語言?

答:Java嚴格按值傳遞,但它在堆棧上傳遞值,對於對象而言,該值恰好是對堆中對象的引用。

在您的問題上下文中,這意味着將臨時對象添加到測試圖中時,實際上是在添加對臨時對象的引用。 這意味着,當您隨后進一步修改臨時對象時,您正在修改的對象與添加到測試映射中的對象相同,因為對臨時對象的引用與您添加到映射中的臨時對象相同。 索引0和1的值相同的原因是因為它們實際上是同一對象。

這是發生在您以相同的溫度進行測試時。 當您放入temp時 ,它將更改其先前的鍵值對以及test的鍵0的值。

您寫過清除了臨時圖。 確定,清除將刪除映射的所有鍵值對,就像c ++一樣。 但是清除溫度也刪除了測試 0鍵的值。 然后,當您再次放入temp時 ,它將更改test的鍵0的先前值(如果清除temp,則為空)。

我只是用一些印刷品編輯了您的代碼。 希望它對您有所幫助。

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("After first put:");
    System.out.println("temp is: " + temp);
    System.out.println("test is: " + test);

    temp.clear();
    System.out.println("After clearing it:");
    System.out.println("temp is: " + temp);
    System.out.println("test is: " + test);

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

    System.out.println("After second put in temp, you haven't put in test yet:");
    System.out.println("temp is: " + temp);
    System.out.println("test is: " + test);
    test.put(1,temp);

    System.out.println("After second put:");
    System.out.println("temp is: " + temp);
    System.out.println("test is: " + test);
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM