4

I have a List<Map<String,String>> such as:

Map<String, String> m1 = new HashMap<>();
m1.put("date", "2020.1.5");
m1.put("B", "10");

Map<String, String> m2 = new HashMap<>();
m2.put("date", "2020.1.5");
m2.put("A", "20");

Map<String, String> m3 = new HashMap<>();
m3.put("date", "2020.1.6");
m3.put("A", "30");

Map<String, String> m4 = new HashMap<>();
m4.put("date", "2020.1.7");
m4.put("C", "30");

List<Map<String, String>> before = new ArrayList<>();
before.add(m1);
before.add(m2);
before.add(m3);
before.add(m4);

My expect result is to generate a new List map, which is grouped by date , and all the entry set in the same date would be put together, like:

[{"A":"20","B":"10","date":"2020.1.5"},{"A":"30","date":"2020.1.6"},{"C":"30","date":"2020.1.7"}]

I tried with the following method, but always not my expect result.

stream().flatmap().collect(Collectors.groupingBy())

Some Additional Comments for this problem:

I worked this out with for LOOP, but the application hangs when the list size is about 50000, so I seek a better performant way to do this. Java 8 stream flat map is a perhaps way as far as I know. So the key point is not only to remap this but also with the most performant way to do this.

13
  • 5
    I have a question about your expected result, do you really want the date key and the date value in the same map as your values? why can you not have a Map<String, List<Map<String, String>> where the first map is keyed on date, and the List<Map<>> is your actual values?
    – scigs
    Commented Jun 15, 2020 at 14:11
  • I'll also ask, why do you need to do this with java 8 streams? I doubt it would be more performant, and you could definitely do it in a more readable way just by hand-rolling the steps through the code.
    – BeUndead
    Commented Jun 15, 2020 at 14:16
  • @BeUndead not sure why you think a Java 8 solution is not readable. Not so performant, perhaps. But definitely not ugly code. Commented Jun 15, 2020 at 14:34
  • @scigs, actually a list is indeed my expect return, not a map
    – Better Man
    Commented Jun 15, 2020 at 14:50
  • 1
    @SilviuBurcea, already done this. I'm just back from a serious problem. Sorry for the late reply.
    – Better Man
    Commented Jul 1, 2020 at 5:13

4 Answers 4

7
before
  .stream()
  .collect(Collectors.toMap((m) -> m.get("date"), m -> m, (a,b) -> {
      Map<String, String> res = new HashMap<>();
      res.putAll(a);
      res.putAll(b);
      return res;
  }))
  .values();

This is the solution you're looking for.

The toMap function receives 3 parameters:

  • the key mapper, which in your case is the date
  • the value mapper, which is the map itself that's being processed
  • the merge function, which takes 2 maps with the same date and puts all the keys together

Output:

[{date=2020.1.5, A=20, B=10}, {date=2020.1.6, A=30}, {date=2020.1.7, C=30}]
4

You can do this way using groupingBy and Collector.of

List<Map<String, String>> list = new ArrayList<>(before.stream()
        .collect(Collectors.groupingBy(
                k -> k.get("date"),
                Collector.of( HashMap<String,String>::new,
                        (m,e)-> m.putAll(e),
                        (map1,map2)->{ map1.putAll(map2); return map1;}
                ))).values());

Here, first use Collectors.groupingBy to group by date. Then define custom collector using Collector.of to collect List<Map<String, String>> into Map<String, String>. After create list using map values.

And using Collectors.flatMapping from Java 9

List<Map<String, String>> list = new ArrayList<>(before.stream()
        .collect(Collectors.groupingBy(
                k -> k.get("date"),
                Collectors.flatMapping(m -> m.entrySet().stream(), 
                    Collectors.toMap(k -> k.getKey(), v -> v.getValue(), (a,b) -> a))))
               .values());
0
1

You can achieve the very same result using a certain number of Collectors, orderly:

  • Collectors.groupingBy to group by the date
  • Collectors.reducing to merge the Map<String, String> items
  • Collectors.collectingAndThen to transform the values from Map<String, Optional<Map<String, String>>>, as a result of the previous reducing to the final output List<Map<String, String>>.
List<Map<String, String>> list = before.stream()
    .collect(Collectors.collectingAndThen(
        Collectors.groupingBy(
            m -> m.get("date"),
            Collectors.reducing((l, r) -> {
                l.putAll(r);
                return l; })
        ),
        o -> o.values().stream()
                       .flatMap(Optional::stream)
                       .collect(Collectors.toList())));

The list contains what are you looking for:

[{date=2020.1.5, A=20, B=10}, {date=2020.1.6, A=30}, {date=2020.1.7, C=30}]

Important: This solution has two he disadvantages:

  • It looks clumsy and might not be clear for an independent viewer
  • It mutates (modifies) the original maps included in the List<Map<String, String>> before.
2
  • 3
    Warning: This solution mutates the original maps! Commented Jun 15, 2020 at 14:38
  • I have included the note. Thanks.
    – Nikolas
    Commented Jun 15, 2020 at 14:43
0

It can be done as follows:

List<Map<String, String>> remapped = before.stream()
    .collect(Collectors.groupingBy(m -> m.get("date")))
    .values().stream()
    .map(e -> e.stream()
               .flatMap(m -> m.entrySet().stream())
               .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, (x1, x2) -> x1)))
    .collect(Collectors.toList());

remapped.forEach(System.out::println);

Output:

{date=2020.1.5, A=20, B=10}
{date=2020.1.6, A=30}
{date=2020.1.7, C=30}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.