Java 根据数值对HashMap进行排序 您所在的位置:网站首页 python二进制拼接 Java 根据数值对HashMap进行排序

Java 根据数值对HashMap进行排序

2023-02-28 15:39| 来源: 网络整理| 查看: 265

Java 根据数值对HashMap进行排序

给出一个学生在100个科目中的分数,其中科目的名称是关键,分数是值。我们的任务是根据数值对HashMap进行排序,即根据分数进行排序。

示例:

输入 : Key = Math, Value = 98 Key = Data Structure, Value = 85 Key = Database, Value = 91 Key = Java, Value = 95 Key = Operating System, Value = 79 Key = Networking, Value = 80

输出 : Key = Operating System, Value = 79 Key = Networking, Value = 80 Key = Data Structure, Value = 85 Key = Database, Value = 91 Key = Java, Value = 95 Key = Math, Value = 98

解决方案: 我们的想法是将条目集存储在一个列表中,并根据数值对列表进行排序。然后从列表中获取值和键,并将其放入一个新的哈希玛。这样,一个新的HashMap就根据值进行了排序。 以下是上述想法的实现:

// Java program to sort hashmap by values import java.util.*; import java.lang.*;   public class GFG {       // function to sort hashmap by values     public static HashMap sortByValue(HashMap hm)     {         // Create a list from elements of HashMap         List list =                new LinkedList(hm.entrySet());           // Sort the list         Collections.sort(list, new Comparator() {             public int compare(Map.Entry o1,                                Map.Entry o2)             {                 return (o1.getValue()).compareTo(o2.getValue());             }         });                   // put data from sorted list to hashmap         HashMap temp = new LinkedHashMap();         for (Map.Entry aa : list) {             temp.put(aa.getKey(), aa.getValue());         }         return temp;     }       // Driver Code     public static void main(String[] args)     {           HashMap hm = new HashMap();           // enter data into hashmap         hm.put("Math", 98);         hm.put("Data Structure", 85);         hm.put("Database", 91);         hm.put("Java", 95);         hm.put("Operating System", 79);         hm.put("Networking", 80);         Map hm1 = sortByValue(hm);           // print the sorted hashmap         for (Map.Entry en : hm1.entrySet()) {             System.out.println("Key = " + en.getKey() +                           ", Value = " + en.getValue());         }     } }

输出

Key = Operating System, Value = 79 Key = Networking, Value = 80 Key = Data Structure, Value = 85 Key = Database, Value = 91 Key = Java, Value = 95 Key = Math, Value = 98

使用Java 8 Lambdas

这里我们将改变我们的排序方式,使用lambda表达式进行排序。逻辑是一样的,甚至我们也传递了比较器对象,但只是使用lambda。

下面是上述方法的实现。

// Java program to sort hashmap by values import java.lang.*; import java.util.*;   public class GFG {       // function to sort hashmap by values     public static HashMap     sortByValue(HashMap hm)     {         // Create a list from elements of HashMap         List list             = new LinkedList(                 hm.entrySet());           // Sort the list using lambda expression         Collections.sort(             list,             (i1,              i2) -> i1.getValue().compareTo(i2.getValue()));           // put data from sorted list to hashmap         HashMap temp             = new LinkedHashMap();         for (Map.Entry aa : list) {             temp.put(aa.getKey(), aa.getValue());         }         return temp;     }       // Driver Code     public static void main(String[] args)     {           HashMap hm             = new HashMap();           // enter data into hashmap         hm.put("Math", 98);         hm.put("Data Structure", 85);         hm.put("Database", 91);         hm.put("Java", 95);         hm.put("Operating System", 79);         hm.put("Networking", 80);         Map hm1 = sortByValue(hm);           // print the sorted hashmap         for (Map.Entry en :              hm1.entrySet()) {             System.out.println("Key = " + en.getKey()                                + ", Value = "                                + en.getValue());         }     } }

输出

Key = Operating System, Value = 79 Key = Networking, Value = 80 Key = Data Structure, Value = 85 Key = Database, Value = 91 Key = Java, Value = 95 Key = Math, Value = 98

在Java 8中使用流

这里我们将使用流来对地图进行排序。我们将使用 stream() 方法来获取entrySet的流,然后在sorted()方法中使用lambda表达式来对流进行排序,最后,我们将使用toMap() 方法将其转换成map。在toMap()方法中,我们使用 LinkedHashMap::new 方法引用来保留地图的排序顺序。

// Java program to sort hashmap by values import static java.util.stream.Collectors.*;   import java.lang.*; import java.util.*; import java.util.stream.*; import java.util.stream.Collectors; public class gfg3 {       // function to sort hashmap by values     public static HashMap     sortByValue(HashMap hm)     {         HashMap temp             = hm.entrySet()                   .stream()                   .sorted((i1, i2)                               -> i1.getValue().compareTo(                                   i2.getValue()))                   .collect(Collectors.toMap(                       Map.Entry::getKey,                       Map.Entry::getValue,                       (e1, e2) -> e1, LinkedHashMap::new));           return temp;     }       // Driver Code     public static void main(String[] args)     {           HashMap hm             = new HashMap();           // enter data into hashmap         hm.put("Math", 98);         hm.put("Data Structure", 85);         hm.put("Database", 91);         hm.put("Java", 95);         hm.put("Operating System", 79);         hm.put("Networking", 80);         Map hm1 = sortByValue(hm);           // print the sorted hashmap         for (Map.Entry en :              hm1.entrySet()) {             System.out.println("Key = " + en.getKey()                                + ", Value = "                                + en.getValue());         }     } }

输出

Key = Operating System, Value = 79 Key = Networking, Value = 80 Key = Data Structure, Value = 85 Key = Database, Value = 91 Key = Java, Value = 95 Key = Math, Value = 98


【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有