Java 8 : grouping by multiple fields and then sort based on value

标签 java lambda groupingby

我有一个类叫做 Info

public class Info {
    private String account;
    private String opportunity;
    private Integer value;
    private String desc;
}

我想按两个字段(帐户和机会)对 Info 列表进行分组,然后根据值对其进行排序。 IE。 分组排序前的信息列表:

List<Info> infos = [(account = 1234, opportunity = abs, value = 4, desc= test),
(account = 1234, opportunity = abs, value = 5, desc = testing),
(account = 1234, opportunity = abss, value = 10, desc = vip),
(account = 123, opportunity = abss, value = 8, desc = vip),
(account = 12, opportunity = absddd, value = 4, desc = production),
(account = 12, opportunity = absooo, value = 2, desc = test)]

分组排序后我期望的结果,

Map<String, Map<String, List<Info>>> result = {
    1234 = {
        abss = [(account = 1234, opportunity = abss, value = 10, desc = vip)],
        abs = [(account = 1234, opportunity = abs, value = 5, desc = testing),  
               (account = 1234, opportunity = abs, value = 4, desc = test)]
    },
    123 = {
        abss = [(account = 123, opportunity = abss, value = 8, desc = vip)]
    },
    12 = {
        absddd = [(account = 12, opportunity = absddd, value = 4, desc = production)],
        absooo = [(account = 12, opportunity = absooo, value = 2, desc = test)]
    }
}

o/p是根据value(10->(5+4)->8->4->2)排序

到目前为止我已经尝试过 = infos.stream().collect(Collectors.groupingBy(Info::getAccount, Collectors.groupingBy(r -> r.getOpportunity(), Collectors.toList()))) 但它是随机排序的。

最佳答案

根据 Infovalue 进行排序,

  1. 制作流sorted ,通过比较,这样分组就会按顺序执行。
  2. 调用groupingBy时, 指定 LinkedHashMapmapFactory 中保留插入顺序。

下面的程序演示了如何实现。


import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

public class Info {
    private String account;
    private String opportunity;
    private Integer value;
    private String desc;

    private Info(String account, String opportunity, Integer value, String desc) {
        super();
        this.account = account;
        this.opportunity = opportunity;
        this.value = value;
        this.desc = desc;
    }

    public static void main(String[] args) {
        List<Info> infos = new ArrayList<>();
        infos.add(new Info("12", "absddd", 4, "production"));
        infos.add(new Info("1234", "abss", 10, "vip"));
        infos.add(new Info("1234", "abs", 4, "test"));
        infos.add(new Info("1234", "abs", 5, "testing"));
        infos.add(new Info("123", "abss", 8, "vip"));
        infos.add(new Info("12", "absooo", 2, "test"));
        Map<String, Map<String, List<Info>>> sortedResult = infos.stream().sorted(Info::compareByValueDesc)
                .collect(Collectors.groupingBy(Info::getAccount, LinkedHashMap::new,
                        Collectors.groupingBy(r -> r.getOpportunity(), LinkedHashMap::new, Collectors.toList())));
        sortedResult.forEach((key, value) -> System.out.println(key + value.toString()));
    }

    public static int compareByValueDesc(Info other1, Info other2) {
        return -other1.value.compareTo(other2.value);
    }

    public String getAccount() {
        return account;
    }

    public void setAccount(String account) {
        this.account = account;
    }

    public String getOpportunity() {
        return opportunity;
    }

    public void setOpportunity(String opportunity) {
        this.opportunity = opportunity;
    }

    public String toString() {
        return this.value.toString();
    }
}

关于Java 8 : grouping by multiple fields and then sort based on value,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63878179/

相关文章:

java - 在 Java 中初始化最终变量的问题

c# - 为 select 中的表达式提取 lambda 片段

java - 流收集然后获取最新记录求和一个值

java - Guava Collections2.transform 不起作用

java - EventBus - 事件生命周期的一些问题

java - 在 java 中模拟 Cipher 时出错

intellij-idea - 在 lambda 中进行转换,在 IntelliJ 中标记为冗余

java - 通过 Stream API 按自定义字段选择不同的对象

java - 如何在应用嵌套分组时保留所有子组通过收集器