Home > Software engineering >  How to use group By for a map in java?
How to use group By for a map in java?

Time:12-25

I have list which contains a map List<Map<String, Object>> map has two keys which is a id and the amount. 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. 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

values for id = 1, 2, 3 ,4 ,5, 4

values for amount = 10000, 450000, 25000, 45000, 35000, 75000

list should sorted as follows(index of list) = 6, 4 (id is large), 2, 5, 3, 1

CodePudding user response:

I Think Using CustomObject instead of Map will be help you solve this in much easy way.

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<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:

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

CodePudding user response:

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:

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