Convert in java List<POJO>
to Map<String, List<Object>>
where the key is a field name and value is a list of values by field.
class Train {
public final String source;
public final String destination;
public final double cost;
public Train(String source, String destination, double cost) {
this.source = source;
this.destination = destination;
this.cost = cost;
}
}
For example:
List<Train> trains = Arrays.asList(
new Train("A", "B", 10.0),
new Train("C", "D", 20.0),
new Train("E", "F", 30.0)
);
It should be converted to:
Map<String, List<Object>> = ... // {source=[A, C, E], destination=[B, D, F], cost=[10.0, 20.0, 30.0]}
UPD: Not a duplicate of #20363719 since it doesn't tell exactly how to transform data but only implies using Collectors.toMap