Java 程序按值对 Map 进行排序

javacampus interviewserver side programmingprogramming更新于 2024/8/8 19:34:00

在本文中,我们将了解如何按值对 Map 进行排序。Java HashMap 是 Java Map 接口的基于哈希表的实现。它是键值对的集合。

下面是相同的演示 −

假设我们的输入是

输入HashMap:
Key = Java, Value = 45
Key = Scala, Value = 20
Key = Mysql, Value = 11
Key = Python, Value = 75

期望输出的将是

排序后的HashMap为:
Key = Mysql, Value = 11
Key = Scala, Value = 20
Key = Java, Value = 45
Key = Python, Value = 75

算法

步骤1 - 开始
步骤2 - 声明
步骤3 - 定义值。
步骤4 - 使用‘sort’方法对映射中的元素进行排序。
步骤5 - 使用‘getValue’方法获取值,并使用‘compareTo’方法比较两个值。
步骤 6 - 遍历哈希图,并使用‘getKey’方法将值放入‘temp’中。使用‘temp’作为返回值。
步骤 7 - 显示结果
步骤 8 - 停止

示例 1

在这里,我们将所有操作都绑定在‘main’函数下。

import java.util.*;
import java.lang.*;
public class Demo {
   public static HashMap<String, Integer> sort(HashMap<String, Integer> input_map){
      List<Map.Entry<String, Integer> > list =
      new LinkedList<Map.Entry<String, Integer> >(input_map.entrySet());
      Collections.sort(list, new Comparator<Map.Entry<String, Integer> >() {
         public int compare(Map.Entry<String, Integer> o1,
         Map.Entry<String, Integer> o2){
            return (o1.getValue()).compareTo(o2.getValue());
         }
      });
      HashMap<String, Integer> temp = new LinkedHashMap<String, Integer>();
      for (Map.Entry<String, Integer> aa : list) {
         temp.put(aa.getKey(), aa.getValue());
      }
      return temp;
   }
   public static void main(String[] args){
      HashMap<String, Integer> input_map = new HashMap<String, Integer>();
      input_map.put("Java", 45);
      input_map.put("Scala", 20);
      input_map.put("Mysql", 11);
      input_map.put("Python", 75);
      System.out.println("The HashMap is defined as:");
      for (Map.Entry<String, Integer> element : input_map.entrySet()) {
         System.out.println("Key = " + element.getKey() + ", Value = " + element.getValue());
      }
      Map<String, Integer> result_map = sort(input_map);
      System.out.println("\nThe HashMap after sorting is:");
      for (Map.Entry<String, Integer> element : result_map.entrySet()) {
         System.out.println("Key = " + element.getKey() + ", Value = " + element.getValue());
      }
   }
}

输出

The HashMap is defined as:
Key = Java, Value = 45
Key = Scala, Value = 20
Key = Mysql, Value = 11
Key = Python, Value = 75

The HashMap after sorting is:
Key = Mysql, Value = 11
Key = Scala, Value = 20
Key = Java, Value = 45
Key = Python, Value = 75

示例 2

在这里,我们将操作封装成展现面向对象编程的函数。

import java.util.*;
import java.lang.*;
public class Demo {
   public static void main(String[] args){
      HashMap<String, Integer> input_map = new HashMap<String, Integer>();
      input_map.put("Java", 45);
      input_map.put("Scala", 20);
      input_map.put("Mysql", 11);
      input_map.put("Python", 75);
      System.out.println("The HashMap is defined as:");
      for (Map.Entry<String, Integer> element : input_map.entrySet()) {
         System.out.println("Key = " + element.getKey() + ", Value = " + element.getValue());
      }
      List<Map.Entry<String, Integer> > list =
      new LinkedList<Map.Entry<String, Integer> >(input_map.entrySet());
      Collections.sort(list, new Comparator<Map.Entry<String, Integer> >() {
         public int compare(Map.Entry<String, Integer> o1,
         Map.Entry<String, Integer> o2){
            return (o1.getValue()).compareTo(o2.getValue());
         }
      });
      HashMap<String, Integer> result_map = new LinkedHashMap<String, Integer>();
      for (Map.Entry<String, Integer> aa : list) {
         result_map.put(aa.getKey(), aa.getValue());
      }
      System.out.println("\nThe HashMap after sorting is:");
      for (Map.Entry<String, Integer> element : result_map.entrySet()) {
         System.out.println("Key = " + element.getKey() + ", Value = " + element.getValue());
      }
   }
}

输出

The HashMap is defined as:
Key = Java, Value = 45
Key = Scala, Value = 20
Key = Mysql, Value = 11
Key = Python, Value = 75

The HashMap after sorting is:
Key = Mysql, Value = 11
Key = Scala, Value = 20
Key = Java, Value = 45
Key = Python, Value = 75

相关文章