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.
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.
Comments