-3

Can someone help me to code for merging equal sized map of list into a list of map?

Ex: Input

Map<String, List<Object>> map = new HashMap<>();
        map.put("userid", Arrays.asList(1, 2, 3));
        map.put("username", Arrays.asList("a","b","c"));
        map.put("country", Arrays.asList("UK","IN","US"));

{country=[UK, IN, US], userid=[1, 2, 3], username=[a, b, c]}

.. and list goes on with any number of maps.

Desired output in JSON:

[
    {"userid":1, "username":"a", "country":"UK"},
    {"userid":2, "username":"b", "country":"IN"},
    {"userid":3, "username":"c", "country":"US"}
]

I checked this post but it has all the combinations. I am looking something like 1:1 mapping.


Tried with below approach. Might not be the right way and appreciate if someone can help with a better solution.

    ArrayList<Object> myList = new ArrayList<>();
    int mapSize = map.get("userid").size();

    for (int i=0; i< mapSize; i++) {
        Map<String,Object> eachMap = new HashMap<>();
        for (String key : map.keySet())
            //System.out.println(key + "," + map.get(key).get(i));
            eachMap.put(key,map.get(key).get(i));
        myList.add(eachMap);
    }
    JSONArray jsonArray = new JSONArray();
    jsonArray.addAll(myList);
    System.out.println(jsonArray);

Output

[{"country":"UK","userid":1,"username":"a"},{"country":"IN","userid":2,"username":"b"},{"country":"US","userid":3,"username":"c"}]

4
  • 1
    You can make it quite simple: Have a Model class with the properties you need. Then have one loop over an index, in which you create a new model, populating the properties from the respective list[index], then add to result list. Done. Commented Aug 30, 2021 at 6:38
  • I am quite new to Java. How do we define & populate the properties from the respective list[index] ? Commented Aug 30, 2021 at 6:41
  • 2
    Java or JavaScript? Those are two completely different things. The notation you use for the maps in your question is JavaScript, not Java. Commented Aug 30, 2021 at 6:44
  • 1
    Sorry for that. I am not aware of the exact notations. I just modified my question again with proper notation. Commented Aug 30, 2021 at 6:57

1 Answer 1

1

Try this.

public static void main(String[] args) {
    Map<String, List<Object>> map = new HashMap<>();
    map.put("userid", Arrays.asList(1, 2, 3));
    map.put("username", Arrays.asList("a","b","c"));
    map.put("country", Arrays.asList("UK","IN","US"));

    List<Map<String, Object>> output = IntStream.range(0, map.values().iterator().next().size())
        .mapToObj(i -> map.entrySet().stream()
            .map(e -> Map.entry(e.getKey(), e.getValue().get(i)))
            .collect(Collectors.toMap(Entry::getKey, Entry::getValue)))
        .toList();

    System.out.println(output);
}

output:

[{country=UK, userid=1, username=a}, {country=IN, userid=2, username=b}, {country=US, userid=3, username=c}]
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks for the response. I tried to use this code and getting "error: cannot find symbol symbol: method entry(String,Object)" . - onlinegdb.com/1fx6qWtcF
I arranged it for the Java version of this site. GDB online Debugger | Code, Compile, Run, Debug online C, C++
still no luck for me.. :( I already selected Java but getting same error. I tried in intellij in my local , "Cannot resolve symbol 'Entry'"
import java.util.Map.Entry;

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.