Java--hashmap如何根据value排序

java中map根据value排序

在Java中,Map是一种非常常用的数据结构,它通过键值对的形式存储数据,Map本身是无序的,但是在实际应用中,我们有时需要根据Map的值来进行排序,本文将介绍如何使用Java中的Map来实现根据Value排序。

Map转TreeMap

在Java中,可以使用TreeMap来根据HashMap的值进行排序。

import java.util.*;
 
public class SortHashMapByValue {
    public static void main(String[] args) {
        // 创建一个HashMap
        HashMap<String, Integer> map = new HashMap<>();
        map.put("A", 10);
        map.put("B", 5);
        map.put("C", 20);
        map.put("D", 15);
 
        // 使用TreeMap对HashMap的值进行排序
        Map<String, Integer> sortedMap = new TreeMap<>(
            new Comparator<String>() {
                @Override
                public int compare(String k1, String k2) {
                    return map.get(k1) - map.get(k2);
                }
            }
        );
        sortedMap.putAll(map);
 
        // 打印排序后的HashMap
        for (Map.Entry<String, Integer> entry : sortedMap.entrySet()) {
            System.out.println(entry.getKey() + " : " + entry.getValue());
        }
    }
}

Map转换为List

将Map中的键值对转换为List,然后通过对List进行排序来得到排序后的结果。
具体代码如下

package org.example;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

public class hashmap_list {
    public static void main(String[] args) {
        HashMap<String, Integer> map = new HashMap<>();
        map.put("A",10);
        map.put("B",5);
        map.put("C",20);
        map.put("D",15);
        //Map中的键值对转换为List,并通过对List进行排序来得到排序后的结果
        List<Map.Entry<String, Integer>> sortedList =
                map.entrySet().stream().sorted(Map.Entry.comparingByValue()).collect(Collectors.toList());
        for (Map.Entry<String, Integer> stringIntegerEntry : sortedList) {
            System.out.println(stringIntegerEntry.getKey()+","+stringIntegerEntry.getValue());
        }//对value排序
        System.out.println("=======================");
        List<Map.Entry<String, Integer>> sortedList1 =
                map.entrySet().stream().sorted(Map.Entry.comparingByKey()).collect(Collectors.toList());
        for (Map.Entry<String, Integer> stringIntegerEntry : sortedList1) {
            System.out.println(stringIntegerEntry.getKey()+","+stringIntegerEntry.getValue());
        }//对key排序
    }
}

  • 6
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
可以通过将HashMap中的Entry对象转换为List,再通过Collections.sort()方法对List进行排序来实现HashMap根据value进行排序的功能。具体步骤如下: 1.将HashMap中的Entry对象转换为List: ```java List<Map.Entry<K, V>> list = new ArrayList<>(map.entrySet()); ``` 2.通过Collections.sort()方法对List进行排序,其中需要自定义Comparator来实现按照value排序: ```java Collections.sort(list, new Comparator<Map.Entry<K, V>>() { @Override public int compare(Map.Entry<K, V> o1, Map.Entry<K, V> o2) { return o1.getValue().compareTo(o2.getValue()); } }); ``` 3.将排序后的List转换为LinkedHashMap,以保证排序后的顺序不变: ```java Map<K, V> sortedMap = new LinkedHashMap<>(); for (Map.Entry<K, V> entry : list) { sortedMap.put(entry.getKey(), entry.getValue()); } ``` 完整代码如下: ```java public static <K, V extends Comparable<? super V>> Map<K, V> sortByValue(Map<K, V> map) { List<Map.Entry<K, V>> list = new ArrayList<>(map.entrySet()); Collections.sort(list, new Comparator<Map.Entry<K, V>>() { @Override public int compare(Map.Entry<K, V> o1, Map.Entry<K, V> o2) { return o1.getValue().compareTo(o2.getValue()); } }); Map<K, V> sortedMap = new LinkedHashMap<>(); for (Map.Entry<K, V> entry : list) { sortedMap.put(entry.getKey(), entry.getValue()); } return sortedMap; } ``` 调用示例: ```java Map<String, Integer> map = new HashMap<>(); map.put("a", 3); map.put("b", 1); map.put("c", 2); Map<String, Integer> sortedMap = sortByValue(map); System.out.println(sortedMap); // 输出:{b=1, c=2, a=3} ```

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值