[英]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.