-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"}]

Bhargav Rao
  • 50,140
  • 28
  • 121
  • 140
Vick
  • 7
  • 1
  • 7
  • 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. – Fildor Aug 30 '21 at 06:38
  • I am quite new to Java. How do we define & populate the properties from the respective list[index] ? – Vick Aug 30 '21 at 06: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. – Jesper Aug 30 '21 at 06:44
  • 1
    Sorry for that. I am not aware of the exact notations. I just modified my question again with proper notation. – Vick Aug 30 '21 at 06:57

1 Answers1

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}]
  • Thanks for the response. I tried to use this code and getting "error: cannot find symbol symbol: method entry(String,Object)" . - https://onlinegdb.com/1fx6qWtcF – Vick Aug 31 '21 at 07:24
  • I arranged it for the Java version of this site. [GDB online Debugger | Code, Compile, Run, Debug online C, C++](https://www.onlinegdb.com/fork/1fx6qWtcF) –  Aug 31 '21 at 07:40
  • still no luck for me.. :( I already selected Java but getting same error. I tried in intellij in my local , "Cannot resolve symbol 'Entry'" – Vick Aug 31 '21 at 08:23
  • `import java.util.Map.Entry;` –  Aug 31 '21 at 08:25