📜  按键和值对 HashMap 进行排序的Java程序

📅  最后修改于: 2022-05-13 01:55:39.829000             🧑  作者: Mango

按键和值对 HashMap 进行排序的Java程序

HashMap 是一个Java集合,是Java.util 包的一部分。它提供了Java Map 接口的基本实现。它以键、值对的形式存储数据,其中键必须是唯一的,但对值没有限制。如果我们尝试插入重复键,它将替换相应键的元素。

HashMap 按值排序

这个想法是将条目集存储在一个列表中,然后在 Comparator 的帮助下使用 Collections.sort() 方法根据值对列表进行排序。然后从列表中获取每个键的值,然后显示结果。

例子

Java
// Java program to sort Hashmap based on values
 
import java.lang.*;
import java.util.*;
 
public class GFG {
 
    // function to sort hashmap based on values
    public static HashMap
    sortByValue(HashMap hm)
    {
        // Creating a list from elements of HashMap
        List > list
            = new LinkedList >(
                hm.entrySet());
 
        // Sorting the list using Collections.sort() method
        // using Comparator
        Collections.sort(
            list,
            new Comparator >() {
                public int compare(
                    Map.Entry object1,
                    Map.Entry object2)
                {
                    return (object1.getValue())
                        .compareTo(object2.getValue());
                }
            });
 
        // putting the  data from sorted list back to hashmap
        HashMap result
            = new LinkedHashMap();
        for (Map.Entry me : list) {
            result.put(me.getKey(), me.getValue());
        }
 
        // returning the sorted HashMap
        return result;
    }
 
    // Driver Code
    public static void main(String[] args)
    {
        // creating object of HashMap class
        HashMap hashmap
            = new HashMap();
 
        // inserting key-value pair into hashmap
        hashmap.put("five", 5);
        hashmap.put("seven", 7);
        hashmap.put("three", 3);
        hashmap.put("nine", 9);
        hashmap.put("zero", 0);
        hashmap.put("eight", 8);
 
        // sorting the HashMap based on values
        Map map = sortByValue(hashmap);
 
        // print the sorted hashmap(based on values)
        for (Map.Entry entry :
             map.entrySet()) {
            System.out.println("Key : " + entry.getKey()
                               + ", Value : "
                               + entry.getValue());
        }
    }
}


Java
// Java Code to sort Map by key value
import java.util.*;
class GFG {
 
    // This map stores unsorted values
    static HashMap m = new HashMap<>();
 
    // Function to sort map by Key
    public static void sortMapByKey()
    {
        ArrayList sortKeys
            = new ArrayList(m.keySet());
 
        Collections.sort(sortKeys);
 
        // Getting value for each key and displaying
        // results.
        for (Integer x : sortKeys)
            System.out.println("Key = " + x
                               + ", Value = " + m.get(x));
    }
 
    // Driver Code
    public static void main(String args[])
    {
        // putting values in the Map
        m.put(7, "seven");
        m.put(5, "five");
        m.put(1, "one");
        m.put(3, "three");
        m.put(9, "nine");
 
        // Calling the function to sortMapByKey to
        // perform sorting based on keys
        sortMapByKey();
    }
}



输出
Key : zero, Value : 0
Key : three, Value : 3
Key : five, Value : 5
Key : seven, Value : 7
Key : eight, Value : 8
Key : nine, Value : 9

HashMap 按键排序

思路是将HashMap的所有数据放到一个ArrayList中,然后将HashMap的所有key提取到一个ArrayList中。接下来,使用Collections.sort()方法对提取的键进行排序,然后使用 get() 方法为每个键提取其值。最后,地图根据其键进行排序。

例子

Java

// Java Code to sort Map by key value
import java.util.*;
class GFG {
 
    // This map stores unsorted values
    static HashMap m = new HashMap<>();
 
    // Function to sort map by Key
    public static void sortMapByKey()
    {
        ArrayList sortKeys
            = new ArrayList(m.keySet());
 
        Collections.sort(sortKeys);
 
        // Getting value for each key and displaying
        // results.
        for (Integer x : sortKeys)
            System.out.println("Key = " + x
                               + ", Value = " + m.get(x));
    }
 
    // Driver Code
    public static void main(String args[])
    {
        // putting values in the Map
        m.put(7, "seven");
        m.put(5, "five");
        m.put(1, "one");
        m.put(3, "three");
        m.put(9, "nine");
 
        // Calling the function to sortMapByKey to
        // perform sorting based on keys
        sortMapByKey();
    }
}


输出
Key = 1, Value = one
Key = 3, Value = three
Key = 5, Value = five
Key = 7, Value = seven
Key = 9, Value = nine