For example I have a class:
public class Period {
private Integer from;
private Integer to;
}
And there is a list of objects in JSON format:
[
{
"from": 1,
"to": 2
}, {
"from": 3,
"to": 4
}, {
"from": 5,
"to": 6
}
]
I want to collect from the List<Period> periods, all of the values from this two fields into one collection. In my case it would be List<Integer> values. Is there any possibility to use stream on periods list? I was trying to make something like this:
values = periods
.stream()
.mapToInt(p -> p.getFrom())
.boxed()
.collect(Collectors.toList());
values.addAll(
periods
.stream()
.mapToInt(p -> p.getTo())
.boxed()
.collect(Collectors.toList()));
The result of this operation on previous JSON is:
[1,2,3,4,5,6]
Is there any any other option to manipulate over stream to accomplish the same result?