Convert Map<List> to List<NewType> with new java 8 streams

johnco3

I need help using the java 8 streams API to convert

Map<String, List<Entry<Parameter, String>>> inputData

to

List<TestSession> testList

with the following test session

private static class TestSession {
    final String mServiceName;
    final Parameter mParam;
    final String mData;
    public TestSession(
        final String aServiceName,
        final Parameter aParameter,
        final String aData) {
        mServiceName = aServiceName;
        mParam = aParam,
        mData= aData;
    }
}

and

enum Parameter {
    Foo,
    Bar,
    Baz
}

Lets say that input data contains the following

{"ABC", {{Parameter.Foo, "hello"},{Parameter.Bar, "bye"} }
{"DEF", {{Parameter.Baz, "hello1"},{Parameter.Foo, "bye1"} }

I would like the testList to contain

{
   TestSession("ABC", Parameter.Foo, "hello"), 
   TestSession("ABC", Parameter.Bar, "bye"), 
   TestSession("DEF", Parameter.Baz, "hello1"),
   TestSession("DEF", Parameter.Foo, "bye1")
}

The idea is that each TestSession is constructed using the Key from the inputData and the Entry<Parameter, String> from each of the entries in the list.

Holger

As already mentioned in a comment by the user “soon”, this problem can be easily solved with flatMap and map:

List<TestSession> list = mapList.entrySet().stream()
    .flatMap(e1 -> e1.getValue().stream()
        .map(e2 -> new TestSession(e1.getKey(), e2.getKey(), e2.getValue())))
    .collect(Collectors.toList());

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

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

Convert List of Strings into Map using Java-8 Streams API

Construct a new map using List of Maps using Java 8 streams

How to convert List<V> into Map<K, List<V>>, with Java 8 streams and custom List and Map suppliers?

Java-8 Streams: Convert Map<String, List<List<DataType>>> to Map<String, List<DataType>>

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

Java-8 Streams: Convert List<{String,List<String>}> to Map<String, List<String>>

How to convert List of Map into Map of List using Java Streams

Java 8 Streams: List to Map with mapped values

convert List<E> to Map<String, List<String>> using java 8 streams

Using Java 8 streams how do I convert Map<String, List<Something>> to Map<String, Integer>?

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

Java 8 Convert List of List of String to Map

Convert Map<String,List<Person>> to Map<String,List<Employee>>, using Java 8 streams. I did this but how do this without for loop

Java 8 streams - convert List of UUID to List of String considering NULL

How to convert List<Object[]> to Map<String,BigInteger> with Streams & Lambda Java8

convert list to multiple lists and storing it in Map using JAVA 8(streams) based on the data

Transforming a list of Models into a Map with an inner map using Java 8 Streams

Iterating a list inside a map of map using java 8 streams

How does one Convert a List to Map with Java-8 Streams where Key is the ListValue and the Value is the amount of values of that specific list

How to convert a list of objects into a map in Java 8?

Convert List to map using java 8

convert list to map using java8

Java 8: Convert 2 Lists into List of Map

How to convert Map to List in Java 8

how to convert list to map by java8?

Convert list to map using Java8

Convert a list of maps to a map of lists using Java Streams

Create a map of String and sorted list using Java 8 streams API