Map排序工具类(正序,倒序)

/**
 * map工具类
 *
 **/
public class MyMapUtils {


    /**
     * 根据map的key排序
     * @methodName orderByKey
     * @param map 原始map
     * @param comparator 自定义比较
     * @return java.util.Map<K,V>
     */
    public static <K extends Comparable<? super K>, V extends Comparable<? super V>> Map<K, V> orderByKey(
            Map<K, V> map, Comparator<K> comparator) {

        if (CollectionUtil.isEmpty(map)) {

            return map;
        }

        Map<K, V> result = Maps.newLinkedHashMapWithExpectedSize(map.size());

        map.entrySet().stream().sorted(Map.Entry.comparingByKey(comparator))
                .forEachOrdered(e -> result.put(e.getKey(), e.getValue()));

        return result;

    }

    /**
     * 根据map的key排序
     * @methodName orderByKey
     * @param map 原始map
     * @param isDesc 是否降序
     * @return java.util.Map<K,V>
     */
    public static <K extends Comparable<? super K>, V extends Comparable<? super V>> Map<K, V> orderByKey(Map<K, V> map, boolean isDesc) {

        if (CollectionUtil.isEmpty(map)) {

            return map;
        }

        Map<K, V> result = Maps.newLinkedHashMapWithExpectedSize(map.size());

        if (isDesc) {

            map.entrySet().stream().sorted(Map.Entry.<K, V>comparingByKey().reversed())
                    .forEachOrdered(e -> result.put(e.getKey(), e.getValue()));

        } else {

            map.entrySet().stream().sorted(Map.Entry.comparingByKey())
                    .forEachOrdered(e -> result.put(e.getKey(), e.getValue()));
        }

        return result;

    }

    /**
     * 根据map的value排序
     * @methodName orderByValue
     * @param map 原始map
     * @param comparator 自定义比较
     * @return java.util.Map<K,V>
     */
    public static <K extends Comparable<? super K>, V extends Comparable<? super V>> Map<K, V> orderByValue(
            Map<K, V> map,Comparator<V> comparator) {

        if (CollectionUtil.isEmpty(map)) {

            return map;
        }

        Map<K, V> result = Maps.newLinkedHashMapWithExpectedSize(map.size());

        map.entrySet().stream().sorted(Map.Entry.comparingByValue(comparator))
                .forEachOrdered(e -> result.put(e.getKey(), e.getValue()));

        return result;

    }


    /**
     * 根据map的value排序
     * @methodName orderByValue
     * @param map 原始map
     * @param isDesc 是否降序
     * @return java.util.Map<K,V>
     */
    public static <K extends Comparable<? super K>, V extends Comparable<? super V>> Map<K, V> orderByValue(Map<K, V> map, boolean isDesc) {

        if (CollectionUtil.isEmpty(map)) {

            return map;
        }

        Map<K, V> result = Maps.newLinkedHashMapWithExpectedSize(map.size());

        if (isDesc) {

            map.entrySet().stream().sorted(Map.Entry.<K, V>comparingByValue().reversed())
                    .forEachOrdered(e -> result.put(e.getKey(), e.getValue()));

        } else {

            map.entrySet().stream().sorted(Map.Entry.comparingByValue())
                    .forEachOrdered(e -> result.put(e.getKey(), e.getValue()));
        }

        return result;

    }


    public static void main(String[] args) {

        Map<Integer, String> map = new HashMap<>();
        map.put(2, "a");
        map.put(1, "b");
        map.put(3, "c");
        map.put(5, "d");
        map.put(4, "e");

        System.out.println("输入>>>>>>>>>"+map);

        Map<Integer, String> kMap = orderByKey(map, true);

        System.out.println("key倒序>>>>>>>>>"+kMap);

        Map<Integer, String> vMap = orderByValue(map, false);

        System.out.println("value正序>>>>>>>>>"+vMap);



    }


}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值