How to iterate List<Map<String, Object>> and add the key and value dynamically in another hash map in java

marty :

I'm trying to iterate the List<Map<String, Object>> and want to check if the code is "approved" or not - if code is having value "approved" then I would like to add "id" as Key and "date" as Value in another hashMap.

List<Map<String, Object>> prodIds = ((List<Map<String, Object>>) myIds.get("result"));

This prodIds returns below set of records:

[{id=[14766724], Date=[1999-01-01]}, {id=[49295837], code=[approved], Date=[2003-04-01]}]

[{id=[58761474621], code=[approved], Date=[2017-09-30]}, {id=[3368781], code=[Cancelled], Date=[2014-01-01]}, {id=[48843224], code=[Cancelled], Date=[2009-01-01]}]

I want the output something like this: If code is "approved" - my new hash map should have value like below:

map.put("49295837", "2003-04-01")
map.put("58761474621", "2017-09-30")

Java Code

List<Map<String, Object>> prodIds = ((List<Map<String, Object>>) myIds.get("result"));
System.out.println("prodIds : " +prodIds ); 
//  [{id=[14766724], Date=[1999-01-01]}, {id=[49295837], code=[approved], Date=[2003-04-01]}]
// [{id=[58761474621], code=[approved], Date=[2017-09-30]}, {id=[3368781], code=[Cancelled], Date=[2014-01-01]}, {id=[48843224], code=[Cancelled], Date=[2009-01-01]}]


Map<String, String> newMap = new HashMap<>();
    for (Map<String, Object> map : prodIds) { 
        for (Map.Entry<String, Object> entry : map.entrySet()) { 
            String key = entry.getKey();
            System.out.println("Key : " +key);

            String value = (String) entry.getValue();
             System.out.println(" Value : " +value);
        }
    }

I'm having difficulty how to put the key(id) and value(Date) dynamically if the code value is "approved" into new hash map. It would be really helpful if someone can help me with this.

Appreciated your help in advance!

Thanks

WJS :

As best as I can determine by your example, this should work. But for each Map<String,Object> I need to know what Object is (e.g. String, List<>, etc).

I am assuming they are lists. If I'm wrong you will get a ClassCastException

    public static Map<String, String>
            getApproved(List<Map<String, Object>> prodIds) {
        Map<String, String> newMap = new HashMap<>();
        for (Map<String, Object> map : prodIds) {
            if (map.containsKey("code") &&
            ((List<String>) map.get("code")).get(0)
                    .equals("approved")) {
                newMap.put(((List<String>) map.get("id")).get(0),
                        (String) ((List<String>) map.get("Date"))
                                .get(0));
            }
        }
        return newMap;
    }

    Map<String, String> newMap = getApproved(prodIds);
    newMap.entrySet().forEach(System.out::println); 

Prints

58761474621=2017-09-30
49295837=2003-04-01

It would help if you could describe all your data structures. Like what is the Object type of map?

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=343185&siteId=1