簡體   English   中英

我如何處理從另一個類返回的嵌套哈希映射值? 爪哇

[英]How do i acess a nested hash map value that i have return from another class ? Java

我是哈希映射的新手,我試圖在類的一側創建一個嵌套的哈希圖,並創建另一個類以對其進行調用,所以這是我的代碼的樣子

public class Hash {
    private HashMap<String, HashMap<String, String>> wow = new HashMap<String, HashMap<String, String>>();

    public void SetHash(){
        wow.put("key", new HashMap<String, Object>());
        wow.get("key").put("key2", "val2");
    }

    public HashMap GetMap(){
        return wow;
    }
}

在另一類是主類上,它將是這樣的:

public static void main(String[] args) {
   Hash h = new Hash();
   h.SetHash();
   System.out.println(h.GetMap.get("key").get("key2"));
}

但是,當我放置第二個get時,會出現錯誤,因此我不確定這是否可行,或者我是否真的應該將哈希直接放置在主類中。

GetMap是一種方法,而不是屬性,因此您必須使用括號()來引用它:

h.GetMap().get("key")

現在,第二個錯誤。 名為wow Map<String, Map<String, String>包含一個值為Map<String, String>類型的對象的值,因此,在獲取之前,您需要獲取地圖:

Map<String, String> m = (HashMap<String, String>) h.GetMap().get("key");

然后可以打印它:

System.out.println(m.get("key2"));

如果您想要一個ONELINER (目前尚不清楚,但請查看注釋中的說明):

System.out.println(((HashMap<String, String>) h.GetMap().get("key")).get("key2"));
//                  ↑ casting  parenthesis  ↑ (
//                 ↑ this say group IS a map and allow get()       ↑
//                ↑ system.out.println parenthesis                              ↑

注意:還要更改此聲明

wow.put("key", new HashMap<String, Object>());

通過

wow.put("key", new HashMap<String, String>());

最終代碼:

public class Q37066776 {
    public static void main(String[] args) {
        Hash h = new Hash();
        h.SetHash();
        Map<String, String> m = (HashMap<String, String>) h.GetMap().get("key");
        System.out.println(m.get("key2"));
    }

}

class Hash {
    private HashMap<String, HashMap<String, String>> wow = new HashMap<String, HashMap<String, String>>();

    public void SetHash() {
        wow.put("key", new HashMap<String, String>());
        wow.get("key").put("key2", "val2");
    }

    public HashMap GetMap() {
        return wow;
    }
}

在線演示



但是你總是可以

做的更好! :=)

正如安德魯指出的

  • 您可以更改方法的返回值,

但也有許多其他事情,例如:

  • 使用較少具體的對象(用Map代替HashMap
  • 遵循約定( GetMap()將為getMap()
  • 使Hash成為帶有static塊的static

如果必須重寫您的代碼,我的結果將是這樣的:

public class Q37066776 {
    public static void main(String[] args) {
        System.out.println(Hash.getMap().get("key").get("key2"));
    }

}

class Hash {
    private static Map<String, Map<String, String>> wow = new HashMap<String, Map<String, String>>();

    static {
        wow.put("key", new HashMap<String, String>());
        wow.get("key").put("key2", "val2");
    }

    public static Map<String, Map<String, String>> getMap() {
        return wow;
    }
}

您有3個錯誤:

  1. GetMap是一種方法-您需要編寫GetMap()。
  2. 您將內部Map聲明為HashMap<String, String> -無法將內部Map初始化為: wow.put("key", new HashMap<String, Object>()); 將其更改為wow.put("key", new HashMap<String, String>());
  3. 為了從主目錄訪問內部地圖,必須將GetMap的返回值聲明為Map<String, HashMap<String, String>>而不是原始類型。 否則,外部類將不知道外部映射值也是哈希映射。

而不是使用嵌套地圖,您應該使用Google的番石榴表: http : //docs.guava-libraries.googlecode.com/git/javadoc/com/google/common/collect/Table.html

暫無
暫無

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

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