how to convert List<Map<String, String>> to Map<String, List<String>>

DeepakVerma

I have list of map List<Map<String,String>> and want to convert into Map<String, List String>>

Below is the sample code

List<Map<String,String>> recordListMap = new ArrayList<>();

Map<String,String> recordMap_1 = new HashMap<>();
recordMap_1.put("inputA", "ValueA_1");
recordMap_1.put("inputB", "ValueB_1");
recordListMap.add(recordMap_1);

Map<String,String> recordMap_2 = new HashMap<>();
recordMap_2.put("inputA", "ValueA_2");
recordMap_2.put("inputB", "ValueB_2");
recordListMap.add(recordMap_2);

I tried the below approach and with this I am not getting the required result:

Map<String, List<String>> myMaps = new HashMap<>();
for (Map<String, String> recordMap : recordListMap) {
    for (Map.Entry<String, String> map : recordMap.entrySet()) {
        myMaps.put(map.getKey(), List.of(map.getValue()));
    }
}
OutPut: {inputB=[ValueB_2], inputA=[ValueA_2]}

Expected Result:

{inputB=[ValueB_1, ValueB_2], inputA=[ValueA_1, ValueA_2]}
Player Schark

See the Map#compute section

Map#compute

Map#computeIfPresent

Map#computeIfAbsent

Assuming you don't want to use Multimap, something like that should do the trick.

    List<Map<String, String>> list = new ArrayList<>();
    Map<String, List<String>> goal = new HashMap<>();

    for (Map<String, String> map : list) {
        for (Map.Entry<String, String> entry : map.entrySet()) {
            goal.compute(entry.getKey(), (k,v)->{
                List<String> l = v == null ? new ArrayList<>() : v;
                l.add(entry.getValue());
                return l;
            });
        }
    }

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to Convert List<Map<String, Object>> to List<Map<String, String>>

How to Convert a List Into Map of Map Map<String, Map<String,Object>>

how to convert List<Map<String, String>> to List<Map<String, Map<String, String>>>

Convert String to List of Map

How to convert a list of java objects to Map<String, Map<String, String>>

How to convert List<Map<String,Object>> to Map<String, String>?

Convert Map<String,List<String>> to List<Map<String,String>> in Kotlin

Java 8: How to convert List<String> to Map<String,List<String>>?

Convert List<String> to Map<String, String> in Java

Java Convert List<String> to Map<String, String>

How to Convert a Map<String, List<List<String>>> to Map<String, List<String>>

convert map string string to map string list string - Java 7

How to convert Map<String, List<String>> to Set<String> using streams?

How to convert an MultiValueMap<String,String> to Map<String, List<Long>> with Stream?

How to convert an MultiValueMap<String,String> to Map<String, List<Long>> with Stream?

How do i convert a String to a List<Map<String,String>>

How to convert List<NameValuePair> into a List<Map.Entry<String, String>>?

How to convert Object list into a map of <String, List<String>>?

How is this Map of String to String possible as a Map of String to a list of String in Groovy?

Java: how to convert a List<?> to a Map<String,?>

How to convert a list of string to map in Scala?

Convert List<Map<String, Object>> to String[][]

Convert a String to Map(String, List([Int,Int]))

Convert List<String> to Map<String, Integer>

How to convert list map to json string and json to list map

How to Convert a Map<String, List<String>> to Map<String, String> in java 8

Map<String,List<String>> to Pair<String,String>

Converting List<Map<String, List<String>>> to String[][]

Convert List<List<TextValue>> to List<Map<String, String>>