1

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

1 Answer 1

1

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?

Sign up to request clarification or add additional context in comments.

8 Comments

Hi - This won't work rite entry.getKey("code")? Because I used map.entrySet())
Look at my updated answer. I think this will work. But why are there [ ] around your dates? Is that a list of one item. Same thing for code values. I need to know what each of you maps looks like structure wise.
Thanks for your response. But Map<String,String> will not work because my actual structure is Map<String, Object> which prints this - [{id=[14766724], Date=[1999-01-01]}, {id=[49295837], code=[approved], Date=[2003-04-01]}]
What is the type of the Object? Just referring to it as Object is not sufficient. Is it a List, or what? And if so List<String> list<MyClass> list of something else. Be specific!
Its List<MyClass>
|

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.