最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

collections - Need to group by list of list in pojo in java and sort by year and month - Stack Overflow

programmeradmin6浏览0评论

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
  • Hello and welcome. It is expected that you show some research on your part before asking here. For example, have you searched on the internet for "java stream group by"? The first result I got seems promising. – Federico klez Culloca Commented Feb 5 at 10:33
  • 1 If you are going to Java 8+ you should also look at using the java.time classes for dates - things like LocalDate, Year, MonthDay, or even ZonedDateTime. These will make sorting much more reliable than trying to sort date strings. – greg-449 Commented Feb 5 at 10:49
  • 1 If you know how to do this in Java 7, you also know how to do it in Java 8. You don't have to use streams, you know. – Mark Rotteveel Commented Feb 5 at 11:38
  • Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it's hard to tell exactly what you're asking. – Community Bot Commented Feb 5 at 13:30
  • Welcome to Stack Overflow. Please post compilable code. Some would not post an answer with code in it without having run the code first, which we can’t here. You may also want to show your Java 7 solution so we can check if we have understood correctly. – Anonymous Commented Feb 5 at 18:59
 |  Show 4 more comments

1 Answer 1

Reset to default 0
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()))));
发布评论

评论列表(0)

  1. 暂无评论