Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create a map from a list of maps

Tags:

java

hashmap

I have a list of maps.

List<Map<Integer, String>>

The values in the list are, for example

<1, String1>
<2, String2>
<1, String3>
<2, String4>

As an end result, I want a Map>, like

<1, <String1, String3>>
<2, <String2, String4>>

How can I achieve this in Java.

CODE :

List<Map<Integer, String>> genericList = new ArrayList<Map<Integer,String>>();
for(TrackActivity activity : activityMajor){
Map<Integer, String> mapIdResponse = activity.getMapIdResponse();
genericList.add(mapIdResponse);
}

Now this genericList is the input and from this list, based on the same ids I want a

Map<Integer, List<String>> mapIdResponseList

Basically, to club the responses which are String based on the ids, grouping the responses with same id in a list and then creating a new map with that id as the key and the list as its value.

like image 466
Aayush Avatar asked Mar 20 '14 08:03

Aayush


People also ask

Can we convert list to Map?

Converting List to Map is a common task. In this tutorial, we'll cover several ways to do this. We'll assume that each element of the List has an identifier that will be used as a key in the resulting Map.

Can we convert list to Map in Java?

With Java 8, you can convert a List to Map in one line using the stream() and Collectors. toMap() utility methods. The Collectors. toMap() method collects a stream as a Map and uses its arguments to decide what key/value to use.


1 Answers

You can do it the following with Java 8:

private void init() {
    List<Map<Integer, String>> mapList = new ArrayList<>();

    Map<Integer, String> map1 = new HashMap<>();
    map1.put(1, "String1");
    mapList.add(map1);

    Map<Integer, String> map2 = new HashMap<>();
    map2.put(2, "String2");
    mapList.add(map2);

    Map<Integer, String> map3 = new HashMap<>();
    map3.put(1, "String3");
    mapList.add(map3);

    Map<Integer, String> map4 = new HashMap<>();
    map4.put(2, "String4");
    mapList.add(map4);

    Map<Integer, List<String>> response = mapList.stream()
            .flatMap(map -> map.entrySet().stream())
            .collect(
                    Collectors.groupingBy(
                            Map.Entry::getKey, 
                            Collectors.mapping(
                                    Map.Entry::getValue, 
                                    Collectors.toList()
                            )
                    )
            );
    response.forEach((i, l) -> {
        System.out.println("Integer: " + i + " / List: " + l);
    });
}

This will print:

Integer: 1 / List: [String1, String3]
Integer: 2 / List: [String2, String4]

Explanation (heavily warranted), I am afraid I cannot explain every single detail, you need to understand the basics of the Stream and Collectors API introduced in Java 8 first:

  1. Obtain a Stream<Map<Integer, String>> from the mapList.
  2. Apply the flatMap operator, which roughly maps a stream into an already existing stream.
    Here: I convert all Map<Integer, String> to Stream<Map.Entry<Integer, String>> and add them to the existing stream, thus now it is also of type Stream<Map.Entry<Integer, String>>.
  3. I intend to collect the Stream<Map.Entry<Integer, String>> into a Map<Integer, List<String>>.
  4. For this I will use a Collectors.groupingBy, which produces a Map<K, List<V>> based on a grouping function, a Function that maps the Map.Entry<Integer, String> to an Integer in this case.
  5. For this I use a method reference, which exactly does what I want, namely Map.Entry::getKey, it operates on a Map.Entry and returns an Integer.
  6. At this point I would have had a Map<Integer, List<Map.Entry<Integer, String>>> if I had not done any extra processing.
  7. To ensure that I get the correct signature, I must add a downstream to the Collectors.groupingBy, which has to provide a collector.
  8. For this downstream I use a collector that maps my Map.Entry entries to their String values via the reference Map.Entry::getValue.
  9. I also need to specify how they are being collected, which is just a Collectors.toList() here, as I want to add them to a list.
  10. And this is how we get a Map<Integer, List,String>>.
like image 73
skiwi Avatar answered Sep 25 '22 10:09

skiwi