This may be trivial, but I need to ensure I do it the correct way.
I have a person object which has property country_id. I need to group persons based upon country_id and then finally pack then into 1 Map object. The Map should have the key as the country_id and its value as List of person objects that belong to that country_id.
So the flow should be:
- Iterate through person objects
- Find
country_idand put in a list. e.gperson A(id=10)in list_A - Similarly
person B(id-20)in list_B - And then put them in HashMap?
My final o/p should be PersonMap(country_id-<PersonList>,country_id-<PersonList> )
Is this approach correct or is there a better approach?
You can put people in the
Mapas you loop through them. Simply look for the list you need in the map and if you don’t find it create it and put it in the map.Assuming
country_idis anintand not usingPersonMapas I don’t know how that works: