0

Here is what I'm trying to do.

Map<String, List<Address>> mapObj = someService.getSomeAddress();

Using above call I'm getting mapObj of type Map<String, List<Address>>

And I want to send mapObj as a parameter in a another method (that I cannot change) as a LinkedHashMap<String, List<LinkedHashMap>> which does some further processing.

Is there any way that I can solve this problem without affecting data inside mapObj?

asked Dec 12, 2016 at 10:01
1
  • 5
    Well, yes. You need to create a copy anyway. How an Address becomes a List<LinkedHashMap> is unclear, though. And what the order should be is unclear, too. And the fact that the method cares about the concrete type of the map is a smell, too. Commented Dec 12, 2016 at 10:04

1 Answer 1

2

You will need to perform a couple of conversion steps.

  1. Convert all Address objects to LinkedHashMap objects.
  2. Put all Map entries into a LinkedHashMap object.

For 1st one, you can write a utility method some where that can do this conversion. For example,

public static List<LinkedHashMap> addresstoMap(List<Address> addresses)
{
 List<LinkedHashMap> list = new ArrayList<>();
 for(Address a: addresses){
 LinkedHashMap map = new LinkedHashMap();
 // Add address fields to map here
 list.add(map);
 }
 return list;
}

Then, for the 2nd step, you can do this:

LinkedHashMap<String, List<LinkedHashMap>> map = new LinkedHashMap<?,?>();

iterate through the entry sets of mapObj and put them into the above map object.

for (Map.Entry<String, List<Address>> e : m.entrySet()) {
 map.put(e.getKey(), addresstoMap(e.getValue()));
}

The final map object above will contain the correct representation in the LinkedHashMap<String, List<LinkedHashMap>> datatype.

Hope this helps!

answered Dec 12, 2016 at 10:29

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.