我如何从另一个 class 访问我具有 return 的嵌套哈希映射值? Java
How do i acess a nested hash map value that i have return from another class ? Java
我是哈希映射的新手,我试图在 class 的一侧创建一个嵌套的哈希映射并创建另一个 class 来调用它,所以我的代码如下所示喜欢
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;
}
}
另一边 class 是主要的 class 它将是这样的:
public static void main(String[] args) {
Hash h = new Hash();
h.SetHash();
System.out.println(h.GetMap.get("key").get("key2"));
}
但是当我放置第二个 get 时,出现错误,所以我不确定这是否可行,或者我是否应该将哈希直接放置在主 class.
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;
}
}
但你总是可以
做得更好! :=)
正如Andrew
指出的那样
- you can change return of the method,
还有很多其他的东西,比如:
- 使用不太具体的对象(
Map
而不是 HashMap
)
- 遵循约定(
GetMap()
将是 getMap()
)
- 用
static
块 制作Hash
一个static
class
如果我不得不重写你的代码,我的结果会是这样的:
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 个错误:
- GetMap 是一种方法 - 您需要编写 GetMap()。
- 您将内部映射声明为
HashMap<String, String>
- 您无法将内部映射初始化为:wow.put("key", new HashMap<String, Object>());
将其更改为 wow.put("key", new HashMap<String, String>());
- 为了从主地图访问内部地图 - 您必须将 GetMap 的返回值声明为
Map<String, HashMap<String, String>>
而不仅仅是原始类型。否则外层class不知道外层map值也是hash map。
您应该使用 google 的 Guava Table,而不是使用嵌套地图:
http://docs.guava-libraries.googlecode.com/git/javadoc/com/google/common/collect/Table.html
我是哈希映射的新手,我试图在 class 的一侧创建一个嵌套的哈希映射并创建另一个 class 来调用它,所以我的代码如下所示喜欢
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;
}
}
另一边 class 是主要的 class 它将是这样的:
public static void main(String[] args) {
Hash h = new Hash();
h.SetHash();
System.out.println(h.GetMap.get("key").get("key2"));
}
但是当我放置第二个 get 时,出现错误,所以我不确定这是否可行,或者我是否应该将哈希直接放置在主 class.
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;
}
}
但你总是可以
做得更好! :=)
正如Andrew
指出的那样
- you can change return of the method,
还有很多其他的东西,比如:
- 使用不太具体的对象(
Map
而不是HashMap
) - 遵循约定(
GetMap()
将是getMap()
) - 用
static
块 制作
Hash
一个static
class
如果我不得不重写你的代码,我的结果会是这样的:
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 个错误:
- GetMap 是一种方法 - 您需要编写 GetMap()。
- 您将内部映射声明为
HashMap<String, String>
- 您无法将内部映射初始化为:wow.put("key", new HashMap<String, Object>());
将其更改为wow.put("key", new HashMap<String, String>());
- 为了从主地图访问内部地图 - 您必须将 GetMap 的返回值声明为
Map<String, HashMap<String, String>>
而不仅仅是原始类型。否则外层class不知道外层map值也是hash map。
您应该使用 google 的 Guava Table,而不是使用嵌套地图: http://docs.guava-libraries.googlecode.com/git/javadoc/com/google/common/collect/Table.html