【问题标题】:How do i acess a nested hash map value that i have return from another class ? Java如何访问从另一个类返回的嵌套哈希图值?爪哇
【发布时间】:2016-09-01 04:42:45
【问题描述】:

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

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() 上的括号。
  • 此外,标准命名约定是以方法名称开头,包括带有小写字母的 getter。此外,您不应该在 getter 的返回类型中隐藏类型参数。有一些类型检查编译器不能这样做,如果你不这样做,第二个 get 也将不起作用(即使在修复了 Berger 提到的方法调用之后),因为编译器不知道地图的值类型是地图。
  • 您还应该避免在表达式右侧重复引用集合的泛型类型。相反,使用像这样的空菱形运算符:private HashMap&lt;String, HashMap&lt;String, String&gt;&gt; wow = new HashMap&lt;&gt;();

标签: java hash hashmap hashtable


【解决方案1】:

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

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

现在,第二个错误。您的 Map&lt;String, Map&lt;String, String&gt; 名为 wow 包含一个类型为 Map&lt;String, String&gt; 的对象的值,因此,在获取之前,您需要获取地图:

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

然后你就可以打印了:

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

如果你想要一个 ONELINER(不是很清楚,但请查看 cmets 中的解释):

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())
  • 使用 static 块使 Hash 成为 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;
    }
}

【讨论】:

  • 您可以将GetMap() 声明为返回HashMap&lt;String, String&gt;,而不是强制转换原始类型。这也将删除有关使用原始类型的编译警告。
  • 不是真的,你可以声明GetMap() 可能返回Map&lt;String, HashMap&lt;String, String&gt;&gt;
  • +1 进行修复。我不确定我是否会提倡 static 块,但该逻辑可能应该在 main 方法中。不过,这只是一种意见。
  • 是的,有多种重构方式,这就是为什么我添加了这只是一个意见的免责声明。
  • 哇,谢谢大家,这真的很简洁,但以我目前的水平,我需要一些时间来消化整个解决方案,但它确实有效,所以谢谢
【解决方案2】:

你有 3 个错误:

  1. GetMap 是一种方法 - 您需要编写 GetMap()。
  2. 您将内部映射声明为HashMap&lt;String, String&gt; - 您无法将内部映射初始化为:wow.put("key", new HashMap&lt;String, Object&gt;()); 将其更改为wow.put("key", new HashMap&lt;String, String&gt;());
  3. 为了从 main 访问内部映射 - 您必须将 GetMap 的返回值声明为 Map&lt;String, HashMap&lt;String, String&gt;&gt; 而不仅仅是原始类型。否则,外部类不会知道外部映射值也是哈希映射。

您应该使用 google 的 Guava Table,而不是使用嵌套地图: http://docs.guava-libraries.googlecode.com/git/javadoc/com/google/common/collect/Table.html

【讨论】:

    猜你喜欢
    • 2012-11-19
    • 2011-02-14
    • 1970-01-01
    • 2016-03-02
    • 1970-01-01
    • 1970-01-01
    • 2020-07-07
    • 2013-08-02
    • 2014-08-11
    相关资源
    最近更新 更多