本文介绍了java 8 stream如何在结果分组中进行分组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在groupingBy LawInfoType中找到所有游戏的日期最大LawInfo?

how to find max LawInfo by date fo all games in groupingBy LawInfoType?

我有简单的模型:

public enum Game {
    football,
    hokey,
    golf,
    basketball
}

public class LawInfo {
    private Date minDate;
    private State state;
    private LawInfoType textType;
    private String lawInfoText;
    private Game game;
}

public enum LawInfoType {
     big, middle , small;
}

public enum State {
    draft, ready, cancel;
}

主要测试

 List<LawInfo> list = new ArrayList<>();

        LawInfo info = null;
        Random random = new Random(123);
        for (int i = 0; i < 3; i++) {

            for (State state : State.values()) {
                for (LawInfoType lawInfoType : LawInfoType.values()) {
                    for (Game game : Game.values()) {
                        info = new LawInfo(new Date(random.nextLong()), state, lawInfoType, "TEXT", game);
                        list.add(info);
                    }

                }
            }
        }

        Predicate<LawInfo>  isReady = l->l.getState().equals(State.ready);


  Map<LawInfoType, List<LawInfo>> map0 = list.stream()
                .filter(isReady)
                .collect(groupingBy(LawInfo::getTextType)); //!!!????

但是我需要按照游戏的日期分组进入每个组

but I need to get in each group max by date group by games

like this : Map<LawInfoType, List<LawInfo>>

small - > [LawInfo(足球,最长日期),LawInfo(hokey,最大日期),LawInfo (高尔夫,最长日期),LawInfo(篮球,最长日期)]

small->[LawInfo(football, max date),LawInfo(hokey, max date) ,LawInfo(golf, max date) , LawInfo(basketball, max date)]

中间 - > [LawInfo(足球,最长日期),LawInfo(hokey,最大日期), LawInfo(高尔夫,最长日期),LawInfo(篮球,最长日期)]

middle->[LawInfo(football, max date),LawInfo(hokey, max date) ,LawInfo(golf, max date) , LawInfo(basketball, max date)]

大 - > [LawInfo(足球,最长日期),LawInfo(hokey,最大日期) ,LawInfo(高尔夫,最长日期),LawInfo(篮球,最长日期)]

big->[LawInfo(football, max date),LawInfo(hokey, max date) ,LawInfo(golf, max date) , LawInfo(basketball, max date)]

推荐答案

您可以使用

Map<LawInfoType, List<LawInfo>> result = list.stream()
    .filter(l -> l.getState()==State.ready)
    .collect(
        Collectors.groupingBy(LawInfo::getTextType,
            Collectors.collectingAndThen(
                Collectors.groupingBy(LawInfo::getGame,
                    Collectors.maxBy(Comparator.comparing(LawInfo::getMinDate))),
                m -> m.values().stream().map(Optional::get).collect(Collectors.toList())
        )));

result.forEach((k,v) -> {
    System.out.println(k);
    v.forEach(l -> System.out.printf("%14s, %15tF%n", l.getGame(), l.getMinDate()));
});

将打印

big
          golf, 250345012-06-20
    basketball,  53051589-05-19
      football, 177220545-11-30
         hokey, 277009605-05-01
middle
          golf,  24379695-11-03
    basketball, 283700233-08-25
      football, 248125707-04-08
         hokey, 195919793-04-22
small
          golf, 152237339-07-10
    basketball, 269880024-08-24
      football, 285393288-11-14
         hokey, 276036745-09-23

包含您的测试数据。请注意,该范围内的日期值不太适合一致性检查,因为此数据集的其他日期可能看起来就像打印输出中的数字更高,因为年份不会以此格式打印为带符号的数字。我建议生成具有合理值的日期,例如有四位数的正数年...

with your test data. Note that date values in that range are not quite suitable for consistency checks as other dates of this data set may look like having a higher number in printouts because the year is not printed as signed number with this format. I’d recommend to generate dates with reasonable values, e.g. having four digit positive number years…

这篇关于java 8 stream如何在结果分组中进行分组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-27 23:26