我如何访问从另一个类返回的嵌套哈希映射值? Java的

时间:2016-05-06 07:31:02

标签: java hash hashmap hashtable

我是哈希映射的新手,我试图在类的一侧创建一个嵌套的哈希映射,并创建另一个类来调用它,所以这就是我的代码的样子

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时,会出现错误,所以我不确定这是否可行,或者我是否应该将哈希直接放在主类中。

2 个答案:

答案 0 :(得分:5)

GetMap是一种方法,而不是属性,所以你必须用括号()来引用它:

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

现在,第二个错误。您的Map<String, Map<String, String>名为wow的值包含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;
    }
}

WORKING ONLINE DEMO

但你总是可以

做得更好! :=)

正如Andrew

指出的那样
  
      
  • 您可以更改方法的返回
  •   

但也有许多其他的事情:

  • 使用较少具体的对象(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;
    }
}

答案 1 :(得分:1)

您有3个错误:

  1. GetMap是一种方法 - 您需要编写GetMap()。
  2. 您将内部地图声明为HashMap<String, String> - 您无法将内部地图初始化为:wow.put("key", new HashMap<String, Object>()); 将其更改为wow.put("key", new HashMap<String, String>());
  3. 为了从main访问内部地图 - 您必须将GetMap的返回值声明为Map<String, HashMap<String, String>>而不是原始类型。否则,外部类将不知道外部地图值也是哈希映射。
  4. 您应该使用google的Guava Table,而不是使用嵌套地图: http://docs.guava-libraries.googlecode.com/git/javadoc/com/google/common/collect/Table.html