Michael Prabhu
Michael Prabhu

Reputation: 1

Need to group by list of list in pojo in java and sort by year and month

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

Upvotes: -6

Views: 57

Answers (1)

Yuvaraj Sakthivel
Yuvaraj Sakthivel

Reputation: 37

Map<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()))));

Upvotes: 0

Related Questions