简体   繁体   English

如何对 java 中的 map 使用 group By?

[英]How to use group By for a map in java?

I have list which contains a map List<Map<String, Object>> map has two keys which is a id and the amount.我有一个列表,其中包含一个 map List<Map<String, Object>> map 有两个键,一个 id 和一个金额。 I want to sort the list descending order base on the amount key, if the amount is same for two elements in the list then sort it based on the id.我想根据数量键对列表进行降序排序,如果列表中两个元素的数量相同,则根据 id 对其进行排序。 How can I do this?我怎样才能做到这一点?


    Map<String, Object> hm = new HashMap<>();
    hm.put(id, 1);
    hm.put(amount, 25000);
    list.add(hm);

index of list element = 1, 2, 3,4,5,6列表元素的索引 = 1、2、3、4、5、6

values for id = 1, 2, 3,4,5, 4 id = 1、2、3、4、5、4 的值

values for amount = 10000, 450000, 25000, 45000, 35000, 75000金额的值 = 10000、450000、25000、45000、35000、75000

list should sorted as follows(index of list) = 6, 4 (id is large), 2, 5, 3, 1列表应按如下方式排序(列表索引)= 6, 4(id 很大), 2, 5, 3, 1

I Think Using CustomObject instead of Map will be help you solve this in much easy way.我认为使用 CustomObject 而不是 Map 将帮助您以非常简单的方式解决这个问题。

CustomObject自定义对象

//fell free to change data type of fields as per your requirement
public class CustomObject {
    private Integer id;
    private Integer amount;

    public CustomObject(Integer id, Integer amount) {
        this.id = id;
        this.amount = amount;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public Integer getAmount() {
        return amount;
    }

    public void setAmount(Integer amount) {
        this.amount = amount;
    }
}

Now List will contain Object instead of Map which will make sorting much easy.现在 List 将包含 Object 而不是 Map ,这将使排序变得更加容易。

List<CustomObject> list=new ArrayList<>();
        CustomObject customObject1=new CustomObject(1,10000);
        CustomObject customObject2=new CustomObject(2,45000);
        CustomObject customObject3=new CustomObject(3,25000);
        CustomObject customObject4=new CustomObject(4,45000);
        CustomObject customObject5=new CustomObject(5,35000);
        CustomObject customObject6=new CustomObject(6,75000);

        list.add(customObject1);
        list.add(customObject2);
        list.add(customObject3);
        list.add(customObject4);
        list.add(customObject5);
        list.add(customObject6);

        // You can use thenComparing() as much as you required incase you want to compare with multiple field of your custom object
        // for your problem it has only two field
        Collections.sort(list, Comparator.comparing(CustomObject::getAmount)
                .thenComparing(CustomObject::getId).reversed());

        list.forEach(x->{

            System.out.println(x.getId()+" -->" +x.getAmount());
        });

output: output:

6 -->75000
4 -->45000
2 -->45000
5 -->35000
3 -->25000
1 -->10000

There are a lot of possibilities, this one modifies the original list:有很多可能性,这一个修改了原始列表:

Comparator<Map<String, Object>> sortByAmount = Comparator.comparing(m -> (int) m.get("amount"));
Comparator<Map<String, Object>> sortById = Comparator.comparing(m -> (int) m.get("id"));

list.sort(sortByAmount.reversed().thenComparing(sortById));

If you want to preserve the original list, you can use the stream api:如果要保留原始列表,可以使用 stream api:

list.stream().sorted(sortByAmount.reversed().thenComparing(sortById));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM