Is there a way to also convert (while streaming and collecting) a List of Strings to a new List of Dates?
My attempt:
I defined a SimpleDateFormat and tried to use that in the follwing code, but I can't get it working all together.
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("dd.mm.yyyy");
ArrayList<Date> dateList = stringList.stream()
.filter(s -> s.startsWith("<"))
.collect(Collectors.toList());
Solution
Based on the hints to use map, I got a working solution for now that looks like this. Of course any optimization on this is welcomed!
int year = 2016;
int month = 2;
int day = 15;
final List<LocalDate> dateListEarlier = dateList.stream()
.filter(s -> s.startsWith("<"))
.map(s -> s.substring(1).trim())
.map(s -> LocalDate.parse(s, formatter))
.sorted()
.filter(d -> d.isAfter(LocalDate.of(year, month, day)))
.collect(Collectors.toList());
simpleDateFormat. You filter some stuff and that's all. Here are several examples about type conversion in streams.mapis what you are looking for.