0

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:

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

My json input is: {"key":"4e32cd954f31320078c5fd218110c7ca","number":"","unique_key":"001"}

What is the reason and how to solve this?

3
  • What are you trying to do? Commented Mar 28, 2017 at 8:07
  • Do you specifically need a HashSet? What's wrong with the Set<Entry<String, JsonElement>> that's already returned? Commented Mar 28, 2017 at 8:18
  • Actually that was the Original code. The problem is Set is not Serialized which is the requirement. Commented Mar 28, 2017 at 8:29

1 Answer 1

1

HashMap$EntrySet has nothing common with HashSet and because of this casting fails

You should rather iterate over json's entry set and add following values to your 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()));
    }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.