Sorted Map Example – By Value or By Key or By Comparator

Sorted Map By Key Example
package com.sheting.basic.collection.sorted;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.TreeMap;

public class SortByKey {
    public static void main(String... args) {
        Map<Integer, String> random = new HashMap<Integer, String>();
        for (int i = 0; i < 10; i++) {
            random.put((int) (Math.random() * 100), String.valueOf((int) (Math.random() * 100)));
        }

        System.out.println("Initial Map: " + Arrays.toString(random.entrySet().toArray()));

        TreeMap<Integer, String> sorted = new TreeMap<Integer, String>(random);
        System.out.println("Sorted Map: " + Arrays.toString(sorted.entrySet().toArray()));
    }
}
Initial Map: [96=13, 35=53, 21=88, 71=98, 8=86, 29=24, 45=63, 94=54, 31=15]
Sorted Map: [8=86, 21=88, 29=24, 31=15, 35=53, 45=63, 71=98, 94=54, 96=13]
Sorted Map By Key Reverse using comparator Example
package com.sheting.basic.collection.sorted;

import java.util.Arrays;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Map;
import java.util.TreeMap;

public class SortByKeyReverse {
    public static void main(String... args) {
        Map<Integer, String> random = new HashMap<Integer, String>();
        for (int i = 0; i < 10; i++) {
            random.put((int) (Math.random() * 100), String.valueOf((int) (Math.random() * 100)));
        }

        System.out.println("Initial Map: " + Arrays.toString(random.entrySet().toArray()));

        TreeMap<Integer, String> sorted = new TreeMap<Integer, String>(new Comparator<Integer>() {
            @Override
            public int compare(Integer o1, Integer o2) {
                return o2.compareTo(o1);
            }
        });
        sorted.putAll(random);
        System.out.println("Sorted Map: " + Arrays.toString(sorted.entrySet().toArray()));
    }
}
Initial Map: [80=49, 17=84, 50=76, 42=90, 10=37, 12=20, 93=65, 46=53, 30=51]
Sorted Map: [93=65, 80=49, 50=76, 46=53, 42=90, 30=51, 17=84, 12=20, 10=37]
Sorted Map By Value using custom comparator Example

If you want to sort a java.util.Map by value, you can create a custom Comparator.

package com.sheting.basic.collection.sorted;

import java.util.Comparator;
import java.util.Map;

public class ValueComparator implements Comparator<Integer> {
    private Map<Integer, String> map;

    public ValueComparator(Map<Integer, String> map) {
        this.map = map;
    }

    @Override
    public int compare(Integer a, Integer b) {
        return map.get(a).compareTo(map.get(b));
    }

}

Next we can use the custom value comparator to sort a Map by Value.

package com.sheting.basic.collection.sorted;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.TreeMap;

public class SortByValue {
    public static void main(String... args) {
        Map<Integer, String> random = new HashMap<Integer, String>();
        for (int i = 0; i < 10; i++) {
            random.put((int) (Math.random() * 100), String.valueOf((int) (Math.random() * 100)));
        }

        System.out.println("Initial Map: " + Arrays.toString(random.entrySet().toArray()));

        TreeMap<Integer, String> sorted = new TreeMap<Integer, String>(new ValueComparator(random));
        sorted.putAll(random);
        System.out.println("Sorted Map: " + Arrays.toString(sorted.entrySet().toArray()));
    }
}
Initial Map: [32=42, 16=51, 48=89, 85=12, 38=13, 24=9, 25=69, 74=28, 44=9, 77=51]
Sorted Map: [85=12, 38=13, 74=28, 32=42, 16=51, 25=69, 48=89, 24=9]
Java 8 Lambda Sorted Map By Value Example
package com.sheting.basic.collection.sorted;

import java.util.Arrays;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.stream.Collectors;

public class SortByValueLambda {
    public static void main(String... args) {
        Map<Integer, String> random = new HashMap<Integer, String>();
        for (int i = 0; i < 10; i++) {
            random.put((int) (Math.random() * 100), String.valueOf((int) (Math.random() * 100)));
        }

        System.out.println("Initial Map: " + Arrays.toString(random.entrySet().toArray()));

        Map<Integer, String> sortedMap = random.entrySet().stream().sorted(Map.Entry.comparingByValue())
                .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, (e1, e2) -> e1, LinkedHashMap::new));
        System.out.println("Sorted Map: " + Arrays.toString(sortedMap.entrySet().toArray()));
    }
}
Initial Map: [0=56, 32=13, 66=5, 83=48, 20=50, 84=28, 57=64, 26=64, 31=68]
Sorted Map: [32=13, 84=28, 83=48, 66=5, 20=50, 0=56, 57=64, 26=64, 31=68]
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值