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

简介:

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

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.     } 


作者:小峰

来源:51CTO

相关文章
|
26天前
|
Java 程序员
java线程池讲解面试
java线程池讲解面试
49 1
|
5天前
|
XML 缓存 Java
Java大厂面试题
Java大厂面试题
18 0
|
5天前
|
存储 安全 Java
Java大厂面试题
Java大厂面试题
11 0
|
5天前
|
存储 安全 Java
Java大厂面试题
Java大厂面试题
13 0
|
6天前
|
安全 Java
就只说 3 个 Java 面试题 —— 02
就只说 3 个 Java 面试题 —— 02
19 0
|
6天前
|
存储 安全 Java
就只说 3 个 Java 面试题
就只说 3 个 Java 面试题
10 0
|
12天前
|
Java 存储
键值之道:深入学习Java中强大的HashMap(二)
键值之道:深入学习Java中强大的HashMap
20 0
键值之道:深入学习Java中强大的HashMap(二)
|
16天前
|
Java 关系型数据库 MySQL
大厂面试题详解:Java抽象类与接口的概念及区别
字节跳动大厂面试题详解:Java抽象类与接口的概念及区别
40 0
|
25天前
|
存储 缓存 算法
Java入门高频考查基础知识4(字节跳动面试题18题2.5万字参考答案)
最重要的是保持自信和冷静。提前准备,并对自己的知识和经验有自信,这样您就能在面试中展现出最佳的表现。祝您面试顺利!Java 是一种广泛使用的面向对象编程语言,在软件开发领域有着重要的地位。Java 提供了丰富的库和强大的特性,适用于多种应用场景,包括企业应用、移动应用、嵌入式系统等。下是几个面试技巧:复习核心概念、熟悉常见问题、编码实践、项目经验准备、注意优缺点、积极参与互动、准备好问题问对方和知其所以然等,多准备最好轻松能举一反三。
50 0
Java入门高频考查基础知识4(字节跳动面试题18题2.5万字参考答案)
|
29天前
|
Java 程序员 API
java1.8常考面试题
在Java 1.8版本中,引入了很多重要的新特性,这些特性常常成为面试的焦点
42 8