Java 8 collect to Map<String, List<Object>>

emoleumassi :

I have two object. The first one:

public final class Object1 {
    private String a;
    private String b;
    // constructor getter and setter
}

The second one:

public class Object2 {
    private BigDecimal value1;
    private BigDecimal value2;
    // constructor getter and setter
}

I have a Map<Object1, Object2>:

    Object1{a="15", b="XXX"}, Object2{value1=12.1, value2=32.3}
    Object1{a="15", b="YYY"}, Object2{value1=21.1, value2=24.3}
    Object1{a="16", b="AAA"}, Object2{value1=34.1, value2=45.3}
    Object1{a="15", b="BBB"}, Object2{value1=23.1, value2=65.3}
    Object1{a="15", b="DDD"}, Object2{value1=23.1, value2=67.3}
    Object1{a="17", b="CCC"}, Object2{value1=78.1, value2=2.3}
........

I want to group this map with the same a in a list of Object2 like:

a="15", {{value1=12.1, value2=32.3}, {value1=21.1, value2=24.3}, {value1=23.1, value2=65.3}, {value1=23.1, value2=67.3}},
a="16", {{value1=34.1, value2=45.3}}
...

I try something like this:

Map<String, List<Object2>> map1 = map.entrySet()
   .stream()             
   .collect(Collectors.toMap(e -> e.getKey().getA(), list of object with this key);
Eugene :
yourMap.entrySet()
       .stream()
       .collect(Collectors.groupingBy(e -> e.getKey().getA(),
               Collectors.mapping(Entry::getValue, Collectors.toList())))

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Collect List<Object> to Map<String, List<Object>> in Java 8 using stream

Java 8 stream to collect a Map of List of items

Convert List<Map<String, Object>> to Map<String, List<Map<String, Object>>> using Java 8 streams API

How to group List<Map<String,Object>> to Map<String,List<Map<String,Object>> in Java8

Java 8 streams, convert List of object to Map<String, Set<String>>

Converting Map<String,String> to List<Object> in Java8

How to transform a List<Map<String, Object>> into Map<String, List<Object>> using Java 8

Java 8 List<Map<String, Object>> to List<Map<String, Object>> group by key and count by value

Creating a map of String and Object from a List in Java 8

Java 8 Stream - Find greatest amount for Map<String, List<Object>>

Collect list of Integer (List<Integer>) to map with Java 8 Stream API

Java stream collect to Map<String, Map<Integer, List<MyObject>>>

Filter and collect a Map<String, List<Object>> to Map<String, List<Object>> based on a condition

Java8: Collect min, max and avg from List to Map

Collect list of strings to map with Java 8 Stream API

convert list of object into a map of <String,Map<String,Integer>> java8 streams

Java Streams: Issue about collect to a Map<String, Object>

Java 8 List<Foo> to Map<String, Map<String, List<String>>>

Comparing a List<Map<String, String>> to a List<Map<String, Object>> Java

Java 8 Convert List of List of String to Map

Collect Parts of a String representing <img> Tags into a List in Java 8

List<Object[]> to Map<K, V> in java 8

Transform a List<Object> to a Map<String,Integer> such that the String is not a duplicate value using Java 8 Streams

How to add Map<String, string> into Map<String, List<Object>> in java

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

Java 8 stream to Map<Integer, List<String>>

Java 8 stream to Map<K,List<String>

Convert list of Object to Map<Key, List<Object>> with java 8 stream

Map<String, Map<String, Car>> to List<Car> using Java 8