简体   繁体   English

java.lang.ClassCastException:java.util.HashMap $ EntrySet无法转换为java.util.HashSet

[英]java.lang.ClassCastException: java.util.HashMap$EntrySet cannot be cast to java.util.HashSet

I have following piece of code : 我有以下代码:

JsonObject json = (JsonObject) attVal;
HashSet<Map.Entry<String,JsonElement>> map = (HashSet<Map.Entry<String, JsonElement>>) json.entrySet();

I am getting below exception at line 2: 我在第2行遇到异常:

java.lang.ClassCastException: java.util.HashMap$EntrySet cannot be cast to java.util.HashSet java.lang.ClassCastException:java.util.HashMap $ EntrySet无法转换为java.util.HashSet

My json input is: {"key":"4e32cd954f31320078c5fd218110c7ca","number":"","unique_key":"001"} 我的json输入是: {“ key”:“ 4e32cd954f31320078c5fd218110c7ca”,“ number”:“”,“ unique_key”:“ 001”}

What is the reason and how to solve this? 原因是什么,如何解决呢?

HashMap$EntrySet has nothing common with HashSet and because of this casting fails HashMap $ EntrySet与HashSet没有什么共同之处,因为这种转换失败

You should rather iterate over json's entry set and add following values to your HashSet 您应该遍历json的条目集并将以下值添加到HashSet中

HashSet<Map.Entry<String,JsonElement>> map = new HashSet<>();

for(Map.Entry<String,JsonElement> entry : json.entrySet()){
    if(entry != null) {
        map.put(entry.getKey(), jsonObject.get(entry.getKey()));
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 java.lang.ClassCastException: java.util.HashMap$EntrySet 不能转换为 java.util.Map$Entry - java.lang.ClassCastException: java.util.HashMap$EntrySet cannot be cast to java.util.Map$Entry java.lang.ClassCastException:无法将java.util.HashSet强制转换为Custom类 - java.lang.ClassCastException: java.util.HashSet cannot be cast to Custom Class java.lang.ClassCastException:无法将java.util.HashMap强制转换为java.lang.String - java.lang.ClassCastException: java.util.HashMap cannot be cast to java.lang.String java.lang.ClassCastException:java.util.HashMap无法强制转换为java.lang.Comparable - java.lang.ClassCastException: java.util.HashMap cannot be cast to java.lang.Comparable java.lang.ClassCastException: java.util.HashSet 与 DTO 不兼容 - java.lang.ClassCastException: java.util.HashSet incompatible with DTO java.lang.ClassCastException:无法将java.util.HashMap强制转换为com.jms.testing.spring.InstructionMessage - java.lang.ClassCastException: java.util.HashMap cannot be cast to com.jms.testing.spring.InstructionMessage java.lang.ClassCastException:java.util.HashMap无法转换为自定义数据类 - java.lang.ClassCastException: java.util.HashMap cannot be cast to custom data class java.lang.ClassCastException: class java.util.Z063A5BC470661C3C7909 无法转换 - java.lang.ClassCastException: class java.util.HashMap cannot be cast : SpringBoot Java,Hibernate java.lang.ClassCastException:org.hibernate.collection.PersistentSet无法强制转换为java.util.HashSet - Java, Hibernate java.lang.ClassCastException: org.hibernate.collection.PersistentSet cannot be cast to java.util.HashSet Cassandra Hadoop MapReduce:java.lang.ClassCastException:java.util.HashMap无法转换为java.nio.ByteBuffer - Cassandra Hadoop MapReduce : java.lang.ClassCastException: java.util.HashMap cannot be cast to java.nio.ByteBuffer
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM