哈希图中的哈希图

发布于 2024-11-18 16:06:21 字数 752 浏览 1 评论 0原文

我有一个

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

清眉祭 2024-11-25 16:06:21

我尝试根据你的描述做了一个demo,没有发现任何错误。

    HashMap<String, Object> hashMap = new HashMap<String, Object>();
    hashMap.put("123", "xyz");
    HashMap<Integer, Object> map = new HashMap<Integer, Object>();
    map.put(453, hashMap);
    HashMap<String, Object> newMap = (HashMap<String, Object>) map.get(453);

    System.out.println("Main map "+ hashMap);
    System.out.println("Map inside map "+map);
    System.out.println("Extracted map "+newMap);

它在 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.

    HashMap<String, Object> hashMap = new HashMap<String, Object>();
    hashMap.put("123", "xyz");
    HashMap<Integer, Object> map = new HashMap<Integer, Object>();
    map.put(453, hashMap);
    HashMap<String, Object> newMap = (HashMap<String, Object>) map.get(453);

    System.out.println("Main map "+ hashMap);
    System.out.println("Map inside map "+map);
    System.out.println("Extracted map "+newMap);

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?

以歌曲疗慰 2024-11-25 16:06:21

正如已经指出的,您无法从提取 HashMap 的代码行中得到该错误。但是,您将收到以下代码行的错误:

String s11 = (String)targetFields.get(453);

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:

String s11 = (String)targetFields.get(453);
深海蓝天 2024-11-25 16:06:21

将值放入 HashMap 中

HashMap<String, Map<?,?>> hashMap = new HashMap<String, Map<?,?>>();

Map<String, String> internalHashMap  = new HashMap<String, String>()

internalHashMap.put("TEST_KEY1","TEST_Value1");
internalHashMap.put("TEST_KEY2","TEST_Value2");
internalHashMap.put("TEST_KEY3","TEST_Value3");

hashMap.put("TEST_KEY",internalHashMap);

To put value in HashMap

HashMap<String, Map<?,?>> hashMap = new HashMap<String, Map<?,?>>();

Map<String, String> internalHashMap  = new HashMap<String, String>()

internalHashMap.put("TEST_KEY1","TEST_Value1");
internalHashMap.put("TEST_KEY2","TEST_Value2");
internalHashMap.put("TEST_KEY3","TEST_Value3");

hashMap.put("TEST_KEY",internalHashMap);
菊凝晚露 2024-11-25 16:06:21

而不是:

HashMap <String, Object> grpMap453 = (HashMap)targetFields.get(453);

尝试

HashMap <String, Object> grpMap453 = (HashMap<String,Object>)targetFields.get(453);

Instead of :

HashMap <String, Object> grpMap453 = (HashMap)targetFields.get(453);

Try

HashMap <String, Object> grpMap453 = (HashMap<String,Object>)targetFields.get(453);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文