Java面试题:如何对HashMap按键值排序

开发 后端
Java中HashMap是一种用于存储“键”和“值”信息对的数据结构。不同于Array、ArrayList和LinkedLists,它不会维持插入元素的顺序。

Java中HashMap是一种用于存储“键”和“值”信息对的数据结构。不同于Array、ArrayList和LinkedLists,它不会维持插入元素的顺序。
因此,在键或值的基础上排序HashMap是一个很难的面试问题,如果你不知道如何解决的话。下面让我们看看如何解决这个问题。

[[148031]]

1. HashMap存储每对键和值作为一个Entry<K,V>对象。例如,给出一个HashMap,

  1. Map<String,Integer> aMap = new HashMap<String,Integer>(); 

键的每次插入,都会有值对应到散列映射上,生成一个Entry <K,V>对象。通过使用这个Entry <K,V>对象,我们可以根据值来排序HashMap。

2.创建一个简单的HashMap,并插入一些键和值。

 

  1. ap<String,Integer> aMap = new HashMap<String,Integer>(); 
  2.  
  3.         // adding keys and values 
  4.         aMap.put("Five"5); 
  5.         aMap.put("Seven"7); 
  6.         aMap.put("Eight"8); 
  7.         aMap.put("One",1); 
  8.         aMap.put("Two",2); 
  9.         aMap.put("Three"3); 

3.从HashMap恢复entry集合,如下所示。

  1. Set<Entry<String,Integer>> mapEntries = aMap.entrySet(); 

4.从上述mapEntries创建LinkedList。我们将排序这个链表来解决顺序问题。我们之所以要使用链表来实现这个目的,是因为在链表中插入元素比数组列表更快。

  1. List<Entry<String,Integer>> aList = new LinkedList<Entry<String,Integer>>(mapEntries); 

5.通过传递链表和自定义比较器来使用Collections.sort()方法排序链表。

 

  1. Collections.sort(aList, new Comparator<Entry<String,Integer>>() { 
  2.  
  3.             @Override 
  4.  
  5.             public int compare(Entry<String, Integer> ele1, 
  6.  
  7.                     Entry<String, Integer> ele2) { 
  8.  
  9.                 return ele1.getValue().compareTo(ele2.getValue()); 
  10.  
  11.             } 
  12.  
  13.         }); 

6.使用自定义比较器,基于entry的值(Entry.getValue()),来排序链表。

7. ele1.getValue(). compareTo(ele2.getValue())——比较这两个值,返回0——如果这两个值完全相同的话;返回1——如果***个值大于第二个值;返回-1——如果***个值小于第二个值。

8. Collections.sort()是一个内置方法,仅排序值的列表。它在Collections类中重载。这两种个方法是

 

  1. public static <T extends Comparable<? super T>> void sort(List<T> list) 
  2.  
  3. public static <T> void sort(List<T> list, Comparator<? super T> c) 

9.现在你已经排序链表,我们需要存储键和值信息对到新的映射中。由于HashMap不保持顺序,因此我们要使用LinkedHashMap。

  1. // Storing the list into Linked HashMap to preserve the order of insertion.      
  2. Map<String,Integer> aMap2 = new LinkedHashMap<String, Integer>(); 
  3.         for(Entry<String,Integer> entry: aList) { 
  4.             aMap2.put(entry.getKey(), entry.getValue()); 
  5.         } 

10.完整的代码如下。

 

  1. package com.speakingcs.maps; 
  2.  
  3. import java.util.Collections; 
  4. import java.util.Comparator; 
  5. import java.util.HashMap; 
  6. import java.util.LinkedHashMap; 
  7. import java.util.LinkedList; 
  8. import java.util.List; 
  9. import java.util.Map; 
  10. import java.util.Map.Entry; 
  11. import java.util.Set; 
  12.  
  13. public class SortMapByValues { 
  14.  
  15.     public static void main(String[] args) { 
  16.  
  17.         Map<String,Integer> aMap = new HashMap<String,Integer>(); 
  18.  
  19.         // adding keys and values 
  20.         aMap.put("Five"5); 
  21.         aMap.put("Seven"7); 
  22.         aMap.put("Eight"8); 
  23.         aMap.put("One",1); 
  24.         aMap.put("Two",2); 
  25.         aMap.put("Three"3); 
  26.  
  27.         sortMapByValues(aMap); 
  28.  
  29.     } 
  30.  
  31.     private static void sortMapByValues(Map<String, Integer> aMap) { 
  32.  
  33.         Set<Entry<String,Integer>> mapEntries = aMap.entrySet(); 
  34.  
  35.         System.out.println("Values and Keys before sorting "); 
  36.         for(Entry<String,Integer> entry : mapEntries) { 
  37.             System.out.println(entry.getValue() + " - "+ entry.getKey()); 
  38.         } 
  39.  
  40.         // used linked list to sort, because insertion of elements in linked list is faster than an array list. 
  41.         List<Entry<String,Integer>> aList = new LinkedList<Entry<String,Integer>>(mapEntries); 
  42.  
  43.         // sorting the List 
  44.         Collections.sort(aList, new Comparator<Entry<String,Integer>>() { 
  45.  
  46.             @Override 
  47.             public int compare(Entry<String, Integer> ele1, 
  48.                     Entry<String, Integer> ele2) { 
  49.  
  50.                 return ele1.getValue().compareTo(ele2.getValue()); 
  51.             } 
  52.         }); 
  53.  
  54.         // Storing the list into Linked HashMap to preserve the order of insertion. 
  55.         Map<String,Integer> aMap2 = new LinkedHashMap<String, Integer>(); 
  56.         for(Entry<String,Integer> entry: aList) { 
  57.             aMap2.put(entry.getKey(), entry.getValue()); 
  58.         } 
  59.  
  60.         // printing values after soring of map 
  61.         System.out.println("Value " + " - " + "Key"); 
  62.         for(Entry<String,Integer> entry : aMap2.entrySet()) { 
  63.             System.out.println(entry.getValue() + " - " + entry.getKey()); 
  64.         } 
  65.  
  66.     } 

译文链接:http://www.codeceo.com/article/java-hashmap-value-sort.html
英文原文:How to Sort HashMap Based On Values in Java

 

责任编辑:王雪燕 来源: 码农网
相关推荐

2023-09-12 11:00:38

HashMap哈希冲突

2022-08-29 07:31:48

HashMap线程扩容

2015-09-02 09:32:56

java线程面试

2009-06-06 18:36:02

java面试题

2009-06-06 18:34:05

java面试题

2023-02-17 14:35:15

HashMapNode类型

2013-05-29 10:23:36

Android开发移动开发Java面试题

2020-06-04 14:40:40

面试题Vue前端

2011-03-24 13:27:37

SQL

2023-11-13 07:37:36

JS面试题线程

2018-03-08 18:40:47

Java百度面试题

2018-07-20 09:24:27

Java面试垃圾收集

2014-09-19 11:17:48

面试题

2017-08-29 14:12:16

Java面试题

2012-05-25 10:15:06

Java程序员面试题

2009-06-16 14:03:16

Hibernate面试Hibernate面试

2021-02-23 12:43:39

Redis面试题缓存

2020-11-05 10:01:35

系统设计软件

2015-09-10 08:46:15

Java面试题

2020-08-06 10:45:30

JavaSpring面试题
点赞
收藏

51CTO技术栈公众号