'Java -Type mismatch: cannot convert from ArrayList to Hashmap

HashMap<String, String> cassandraorderInfo=Cassandraorderdetails.fetchorderdetailsofcassandra(order,Session3);

here fetchorderdetailsofcassandra function is defined as below.

public static ArrayList<HashMap<String, String>> fetchorderdetailsofcassandra(String orderid, Session session)

while we run above code getting below error

Type mismatch: cannot convert from ArrayList<HashMap<String,String>> to HashMap<String,String>

how can this issue be resolved how can this be converted when i try to make change at just at 1 line getting errors at multiple places so is there any way to convert this to resolve the issue.



Solution 1:[1]

Depending on what you wrote, you need to understand that the method call of

Cassandraorderdetails.fetchorderdetailsofcassandra(order,Session3);

results in an object of type ArrayList<HashMap<String, String>>

Meaning, this is an arraylist, where each element is a hashmap.

In the code snippet above, you are trying to convert the returned object from this call to a hashmap directly.

HashMap<String, String> cassandraorderInfo=Cassandraorderdetails.fetchorderdetailsofcassandra(order,Session3);

That won't work.

You need to get a specific object in the returned arraylist, as that will be a hashmap.

I.E.

ArrayList<HashMap<String, String>> data = Cassandraorderdetails.fetchorderdetailsofcassandra(order,Session3);
HashMap<String, String> element = data.get(0);

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 tomerpacific