哈希图中的哈希图
我有一个
Map<String, Object> grpFields = new HashMap<String, Object>();
包含在另一个哈希映射中的哈希映射:
Map<Integer, Object> targetFields = new LinkedHashMap<Integer, Object>();
我可以在调试模式下看到它:
20005=0, 453={452-2=7, 452-0=1, 452-1=17, 448-2=81, 448-1=0A, 447-2=D, 447-1=D, 447-0=D, 448-0=0A}, 11=1116744Pq2Q,
其中 453 是哈希映射,但是当尝试使用以下方法从父哈希映射中提取哈希映射时:
HashMap <String, Object> grpMap453 = (HashMap)targetFields.get(453);
我抛出:
java.lang.ClassCastException: java.util.HashMap cannot be cast to java.lang.String
当然是调用 targetFields .get(453);
应该简单地返回一个哈希图吗?
I have a hashmap
Map<String, Object> grpFields = new HashMap<String, Object>();
which is contained within another hashmap:
Map<Integer, Object> targetFields = new LinkedHashMap<Integer, Object>();
I can see it within debug mode:
20005=0, 453={452-2=7, 452-0=1, 452-1=17, 448-2=81, 448-1=0A, 447-2=D, 447-1=D, 447-0=D, 448-0=0A}, 11=1116744Pq2Q,
where 453 is the Hashmap, however when trying to extract the hashmap from the parent hashmap using:
HashMap <String, Object> grpMap453 = (HashMap)targetFields.get(453);
I'm thrown:
java.lang.ClassCastException: java.util.HashMap cannot be cast to java.lang.String
Surely the call targetFields.get(453);
should simply return a hashmap?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我尝试根据你的描述做了一个demo,没有发现任何错误。
它在
HashMap行发出警告。 newMap = (HashMap) map.get(453);
即“类型安全:未检查从对象到 HashMap 的转换”,但根本没有错误。你也在做同样的事吗?
I've tried making a demo on the basis of what you have described and found no error in it.
It gives warning at line
HashMap<String, Object> newMap = (HashMap<String, Object>) map.get(453);
that is "Type safety: Unchecked cast from Object to HashMap" but no error at all.Are you doing the same?
正如已经指出的,您无法从提取 HashMap 的代码行中得到该错误。但是,您将收到以下代码行的错误:
As already noted you cannot get that error from the line of code where you extract the HashMap. However you will receive that error with following line of code:
将值放入 HashMap 中
To put value in HashMap
而不是:
尝试
Instead of :
Try