对Map进行排序,By Key or By Value

目录

1. 代码


1. 代码

import java.util.*;
import java.util.stream.Collectors;

/**
 * @Author: Thomas
 * @Date: 2023-05-14  15:18:03
 * @Description: 对Map进行排序,By Key or By Value
 */
public class treeMapTest01 {
    public static void main(String[] args) {
        Map<String, Integer> map = new TreeMap<>();
        map.put("b2", 234);
        map.put("a2", 23);
        map.put("b1", 4);
        map.put("a1", 555);
        System.out.println(map); // {a1=555, a2=23, b1=4, b2=234}

        // 根据key逆序排序
        reverseOrderPrintByKey();

        // 根据value正序排序
        positiveOrderByValue();

        // 根据value逆序排序
        reverseOrderByValue();
    }

    /**
     * 根据key逆序排序
     */
    public static void reverseOrderPrintByKey(){
        // Lambda表达式
        /**
         * 只能对TreeMap自定义排序,不能对HashMap自定义排序!!!!
         * 如果有需要,可以将HashMap转为TreeMap
         * 1. Map<String, Integer> hashmap = new HashMap<>();
         * 2. hashmap.put("1", 2);
         * 3. Map<String, Integer> sorted = new TreeMap<>(hashmap);
         */
        Map<String, Integer> map = new TreeMap<>((o1, o2) -> -o1.compareTo(o2));
        map.put("b2", 234);
        map.put("a2", 23);
        map.put("b1", 4);
        map.put("a1", 555);
        System.out.println(map); // {b2=234, b1=4, a2=23, a1=555}
    }

    /**
     * 根据value正序排序
     */
    public static void positiveOrderByValue(){
        Map<String, Integer> map = new HashMap<>();
        map.put("b2", 234);
        map.put("a2", 23);
        map.put("b1", 4);
        map.put("a1", 555);
        System.out.println(map);  //{b2=234, a1=555, a2=23, b1=4}
        // 第一步:转为List
        List<Map.Entry<String, Integer>> list = new ArrayList<>(map.entrySet());
        System.out.println("排序前的list: " + list); //[b2=234, a1=555, a2=23, b1=4]

        // 第二步:对list中的Entry对象进行排序
        list.sort(new Comparator<Map.Entry<String, Integer>>() {
            @Override
            public int compare(Map.Entry<String, Integer> o1, Map.Entry<String, Integer> o2) {
                return o1.getValue() - o2.getValue();
            }
        });
        System.out.println("排序后的list: " + list); //[b1=4, a2=23, b2=234, a1=555]

        // 第三步:转为有序的Map(是LinkedHashMap,不是HashMap)
        Map<String, Integer> collect = list.stream().collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, (v1, v2) -> v1, LinkedHashMap::new));
        System.out.println(collect); //{b1=4, a2=23, b2=234, a1=555}
    }

    /**
     * 根据value逆序排序
     */
    public static void reverseOrderByValue(){
        Map<String, Integer> map = new HashMap<>();
        map.put("b2", 234);
        map.put("a2", 23);
        map.put("b1", 4);
        map.put("a1", 555);
        System.out.println(map);  //{b2=234, a1=555, a2=23, b1=4}
        // 第一步:转为List
        List<Map.Entry<String, Integer>> list = new ArrayList<>(map.entrySet());
        System.out.println("排序前的list: " + list); //[b2=234, a1=555, a2=23, b1=4]

        // 第二步:对list中的Entry对象进行排序
        list.sort(new Comparator<Map.Entry<String, Integer>>() {
            @Override
            public int compare(Map.Entry<String, Integer> o1, Map.Entry<String, Integer> o2) {
                return o2.getValue() - o1.getValue();
            }
        });
        System.out.println("排序后的list: " + list); //[a1=555, b2=234, a2=23, b1=4]

        // 第三步:转为有序的Map(是LinkedHashMap,不是HashMap)
        /**
         * 转为HashMap,有序的List会变为无序。 所以只能使用是LinkedHashMap!!!!
         */
        Map<String, Integer> collect = list.stream().collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, (v1, v2) -> v1, LinkedHashMap::new));
        System.out.println(collect); // {a1=555, b2=234, a2=23, b1=4}
    }

}

2. 总结

1. 对key进行自定义排序,请使用TreeMap对象

2. 对Value进行排序,无所谓,只要是Map对象即可;

因为都会将Map对象转为List<Map.Entry<String, Integer>>对象再进行排序!!!!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值