We are getting list of AssetValue from database,
After getting the list I need to group by id and sort by year and month.
Based on the grouping I need to print on reports.
Public Class AssestValue{
private String Date;
Private String Year;
Private Idvalue idvalue;
--Getter and Setters
}
Public Class IdValue{
private String id;
}
I know to do this in Java 7 , but I need to know how to achieve this in Java 8 Streams.
I need to identify how to achieve this in Java streams
We are getting list of AssetValue from database,
After getting the list I need to group by id and sort by year and month.
Based on the grouping I need to print on reports.
Public Class AssestValue{
private String Date;
Private String Year;
Private Idvalue idvalue;
--Getter and Setters
}
Public Class IdValue{
private String id;
}
I know to do this in Java 7 , but I need to know how to achieve this in Java 8 Streams.
I need to identify how to achieve this in Java streams
Share Improve this question edited Feb 9 at 19:21 M. Justin 21.2k10 gold badges127 silver badges161 bronze badges asked Feb 5 at 9:58 Michael PrabhuMichael Prabhu 11 bronze badge 9 | Show 4 more comments1 Answer
Reset to default 0Map<IdValue, List<AssestValue>> groupbyAns = assetValues.stream().collect(Collectors.groupingBy(assestValue -> assestValue.getIdvalue()));
groupbyAns.forEach((idValue, assestValues) -> assestValues
.sort(Comparator.comparingInt((AssestValue o) -> Integer.parseInt(o.getYear()))
.thenComparing((AssestValue o1) -> Integer.parseInt(o1.getDate()))));
LocalDate
,Year
,MonthDay
, or evenZonedDateTime
. These will make sorting much more reliable than trying to sort date strings. – greg-449 Commented Feb 5 at 10:49