0

Below is my code.Any help is appreciated. I am simply not able to read list and create a Map. I am passing a List<Map<String, Object>> as a function parameter till the Rest layer.In Rest Proxy call its a plain List. In my service layer I need to use Map values stored in the List.

My List has values as mentioned

[{ID=56, VALUE=CPR,DESCRIPTOR=HEAD}, 
{ID=68,VALUE=RegFinance,DESCRIPTOR=FINANCE}]

I want a create Map<String,Map> using List<Map<String, Object>> in below format

{56={ID=56, VALUE=CPR,DESCRIPTOR=HEAD}, 
68={ID=68,VALUE=RegFinance,DESCRIPTOR=FINANCE}}

Below code works before rest call is made i.e in Controller layer but does not work in Service layer after rest call.

    Map<String, Object> userRoleMap = new HashMap<>();
                for (int count = 0; count < allRolesDetails.size(); count++) {
                    //Map<String, Object> mapp=allRolesDetails.get(count);
                   //Above line Gives Exception

                    String[] singleColumn = allRolesDetails.get(count).toString().split(",");
                   //Above line Gives Exception

                    for(String pair : singleColumn)  
                    {
                        String[] entry = pair.split("=");   
                        userRoleMap.put(entry[1].trim(),allRolesDetails.get(count)); 
                        break;//add them to the hashmap and trim whitespaces
                    }
                }

Tried all other options on StackoverFlow

Iterator<Map<String, Object>> it = allRolesDetails.iterator();
            while (it.hasNext()) {
                Map<String, Object> map = it.next(); //so here you don't need a potentially unsafe cast
                for (Map.Entry<String, Object> entry : map.entrySet()) {
                    System.out.println(entry.getKey() + " = " + entry.getValue());
                }
            }

And

for(Map<String, Object> map:allRolesDetails){
                for(Map.Entry<String, Object>entry : map.entrySet()){
                    String Key=entry.getKey();
                    Object Value=entry.getValue();
                }
            }

In all the cases I am getting the below exception whenever I am using allRolesDetails.get(count) or trying to user Iterator or Map.Entry.

java.lang.ClassCastException: java.lang.String cannot be cast to java.util.Map

3
  • Welcome to Stack Overflow! Please take the tour to see how the site works and what questions are on topic here, and edit your question accordingly. See also: How to create a Minimal, Complete, and Verifiable example Commented Sep 16, 2017 at 18:49
  • you have two maps ? Commented Sep 16, 2017 at 19:15
  • Yes one is Empty Map userRoleMap<String,Map> in which I want to fill values from List<Map<String,Object>> Commented Sep 17, 2017 at 3:36

2 Answers 2

1

You are typing a LOT of code here, when you could just do this

Map<String, Map<String, Object>> userRoleMap = new HashMap<>();
for (Map<String, Object> m : allRolesDetails) {
    userRoleMap.put(m.get("ID"), m);
}
Sign up to request clarification or add additional context in comments.

2 Comments

Still gives me below exception at for (Map<String, Object> m : allRolesDetails) { java.lang.ClassCastException: java.lang.String cannot be cast to java.util.Map at com.apple.sfo.workflow.service.InventoryServiceImpl.fetchCenterForDetails(InventoryServiceImpl.java:4649) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
After I figured out the problem used above few lines of code instead of a LOT of code which I was using.Thanks for the inputs.
0

I found out the solution. As I mentioned

Below code works before rest call is made i.e in Controller layer but does not work in Service layer after rest call.

There was an Issue with type of parameter I was passing in RestProxy Call I was passing @QueryParam(value = "allRolesDetails") List allRolesDetails instead of final List<Map<String, Object>> allRolesDetails .After this change proper Map was getting retrieved while iterating List. This post helped me find my problem. sending List/Map as POST parameter jersey

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.