How to get the values of a hashmap for a particular key when it has List as values? [closed]

I want to look for the corresponding value of a nested array object in another nested array object. In order to achieve this I have created a hashmap HashMap<String, List<String>> map = new HashMap<>()where I am storing the list of values for a particular key. Now I want to fetch all the values of that corresponding key and use it to compare all the values in the other nested array object.

"data": {
      "user": [
        {
          "Id": "5735",
          "Items": [
            {
              "ItemId": "698726h",
              "state": "werer"
            }
          ]
        }
      ],

The user id is stored as a key and the List of Strings implies the ItemIds of a particular user.

HashMap<String, List<String>> map = new HashMap<>();
if(map.containsKey("xyz")){

//Fetch all the corresponding values as a list

}

//Next I have to iterate through the obtained list and match the corresponding values with the other nested array object .

List<String> val= map.get(position.positionId()); //is showing error

The error is

Incompatible types. Found: ‘java.lang.Object’, required: ‘java.util.List<java.lang.String>’

I want to get the list for a particular key. This will help me to traverse the values in the list but it looks that I am getting an object in return.

  • What error, precisely, are you getting? Is it just the typo that you have one variable named map and then are referring to it as hashMap later?

    – 

  • The error is — “Required type List <java.lang.String> but provided Object” . The map and hashmap are the same just renamed in another function.

    – 




  • You have shown the declaration of map but not the declaration of hashMap. What is the type of hashMap?

    – 




  • Fetch all the corresponding values as a list What does that mean? You already have the list for that key. Do you then want to use that list of strings as keys and continue searching? Please provide input and expected output.

    – 

  • both are HashMap…sorry for the confusion. i have edited the post. The error is –> “Incompatible types. Found: ‘java.lang.Object’, required: ‘java.util.List<java.lang.String>'”

    – 

Leave a Comment