I have a list of objects that contain coordinates and a name. An example of the printed list can be seen below:
[X=0.1, Y=0.1, name=Series1]
[X=0.1, Y=0.2, name=Series1]
[X=0.1, Y=0.3, name=Series1]
[X=0.1, Y=0.4, name=Series2]
[X=0.1, Y=0.5, name=Series2]
What I'm trying to achieve is to group them together based on the name which can be done with the following Java 8 operation:
Map<String, List<Coordinates>> newList = coordinateList.stream()
.collect(Collectors.groupingBy(Coordinates::getName));
Output:
Series1=[[X=0.1, Y=0.1],[X=0.1, Y=0.2],[X=0.1, Y=0.3]]
Series2=[[X=0.1, Y=0.4],[X=0.1, Y=0.5]]
The final step which I have not yet been able to achieve, is to iterate through the Map, create lists of X's and Y's so I can finally create a graph (chart.addSeries(name, xData, yData);).
My approach to this so far has been the following:
List<Double> xData = new LinkedList<Double>();
List<Double> yData = new LinkedList<Double>();
newList.forEach((k, v) -> {
//TO-DO
});
Inside the loop I would like to assign X's and Y's to the corresponding lists but with no success so far.
One of the problems is that X and Y are associated with a Coordinates object, so I cannot just add them to the list which is of type Double. Another problem I faced is that I cannot get X and Y separately from the list. I have tried getX() and getY() but with no luck.
Example of what I would like the lists to contain:
For the key Series1 the generated lists would be the following:
xData=[0.1,0.1,0.1]
yData=[0.1,0.2,0.3]
For the next key Series 2:
xData=[0.1,0.1]
yData=[0.4,0.5]