Java 无法将对象转换为哈希图
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18328504/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Cannot cast object to hashmap
提问by user2698746
My code:
我的代码:
Object res = stub.call(sessionId, "sale.list", "11111");
HashMap<String, String> map = (HashMap<String, String>) res;
Converting object into HashMap i got following exception,
将对象转换为 HashMap 我得到以下异常,
Exception in thread "main" java.lang.ClassCastException: [Ljava.util.HashMap;
cannot be cast to java.util.HashMap
Can anyone help me how to retrieve object data?
谁能帮助我如何检索对象数据?
采纳答案by Juned Ahsan
You can cast res to HashMap
only if stub.call(sessionId, "sale.list", "11111")
returns a HashMap
object.
HashMap
仅当stub.call(sessionId, "sale.list", "11111")
返回一个HashMap
对象时,您才能将 res 转换为。
[Ljava.lang.HashMap;
indicates that what you are getting is an array of hashMap and not a single HashMap.
[Ljava.lang.HashMap;
表示您得到的是一个 hashMap 数组,而不是单个 HashMap。
回答by Waqas Memon
Object res = stub.call(sessionId, "sale.list", "11111");
Now, if you know the above code returns a java.util.Map or atleast a java.util.HashMap, and it follows the same generics then you can cast it to a Map. Or the least, it should return a null.
现在,如果您知道上面的代码返回一个 java.util.Map 或至少一个 java.util.HashMap,并且它遵循相同的泛型,那么您可以将其转换为 Map。或者至少,它应该返回一个空值。
Map<String, String> map = (Map<String, String>) res;
回答by Joni
You are getting an array of hash maps, so cast to array:
您正在获得一个哈希映射数组,因此转换为数组:
HashMap[] maps = (HashMap[]) res;
回答by Ruchira Gayan Ranaweera
If you want to cast some thing to some other thing, you may know about java casting. In Java there are two types of reference variable casting.
如果您想将某些内容转换为其他内容,您可能了解 java 转换。在 Java 中有两种类型的引用变量转换。
Downcasting: If you have a reference variable that refers to a subtype object, you can assign it to a reference variable of the subtype. You must make an explicit cast to do this, and the result is that you can access the subtype's members with this new reference variable.
Downcasting:如果您有一个引用变量引用子类型对象,则可以将其分配给子类型的引用变量。您必须进行显式转换才能执行此操作,结果是您可以使用此新引用变量访问子类型的成员。
Upcasting: You can assign a reference variable to a supertype reference variable explicitly or implicitly. This is an inherently safe operation because the assignment restricts the access capabilities of the new variable.
Upcasting:您可以显式或隐式地将引用变量分配给超类型引用变量。这是一个本质上安全的操作,因为赋值限制了新变量的访问能力。
I quote this from Java rules for casting
我从用于转换的 Java 规则中引用了这一点
回答by Stephen C
The exception message indicates that the stub.call(...)
method is returning HashMap[]
and not HashMap
.
异常消息表明该stub.call(...)
方法正在返回HashMap[]
而不是HashMap
。
The "fix" is to do this:
“修复”是这样做的:
HashMap<String, String>[] maps = (HashMap<String, String>[]) res;
Or if you know that the array will contain exactly one element, then:
或者,如果您知道数组将只包含一个元素,则:
HashMap<String, String> map = ((HashMap<String, String>[]) res)[0];
Unfortunately, both of these are going to give you warnings about an unchecked conversions. and the only way to avoid that is to use wildcard / raw types; e.g.
不幸的是,这两种方法都会向您发出有关未经检查的转换的警告。避免这种情况的唯一方法是使用通配符/原始类型;例如
HashMap<?, ?> map = ((HashMap<?, ?>[]) res)[0];