自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

Java面試題:如何對HashMap按鍵值排序

開發(fā) 后端
Java中HashMap是一種用于存儲“鍵”和“值”信息對的數(shù)據(jù)結(jié)構(gòu)。不同于Array、ArrayList和LinkedLists,它不會維持插入元素的順序。

Java中HashMap是一種用于存儲“鍵”和“值”信息對的數(shù)據(jù)結(jié)構(gòu)。不同于Array、ArrayList和LinkedLists,它不會維持插入元素的順序。
因此,在鍵或值的基礎(chǔ)上排序HashMap是一個很難的面試問題,如果你不知道如何解決的話。下面讓我們看看如何解決這個問題。

[[148031]]

1. HashMap存儲每對鍵和值作為一個Entry<K,V>對象。例如,給出一個HashMap,

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

鍵的每次插入,都會有值對應(yīng)到散列映射上,生成一個Entry <K,V>對象。通過使用這個Entry <K,V>對象,我們可以根據(jù)值來排序HashMap。

2.創(chuàng)建一個簡單的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恢復(fù)entry集合,如下所示。

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

4.從上述mapEntries創(chuàng)建LinkedList。我們將排序這個鏈表來解決順序問題。我們之所以要使用鏈表來實現(xiàn)這個目的,是因為在鏈表中插入元素比數(shù)組列表更快。

  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()是一個內(nèi)置方法,僅排序值的列表。它在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.現(xiàn)在你已經(jīng)排序鏈表,我們需要存儲鍵和值信息對到新的映射中。由于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

 

責(zé)任編輯:王雪燕 來源: 碼農(nóng)網(wǎng)
相關(guān)推薦

2022-08-29 07:31:48

HashMap線程擴(kuò)容

2023-09-12 11:00:38

HashMap哈希沖突

2009-06-06 18:34:05

java面試題

2009-06-06 18:36:02

java面試題

2015-09-02 09:32:56

java線程面試

2023-02-17 14:35:15

HashMapNode類型

2020-06-04 14:40:40

面試題Vue前端

2013-05-29 10:23:36

Android開發(fā)移動開發(fā)Java面試題

2023-11-13 07:37:36

JS面試題線程

2011-03-24 13:27:37

SQL

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面試題

2025-02-26 07:58:41

2023-07-14 08:12:21

計時器unsafecontext

2013-01-05 14:51:34

JavaScriptjQuery面試

2024-06-04 14:52:28

2014-07-28 14:00:40

linux面試題

2021-02-23 12:43:39

Redis面試題緩存
點贊
收藏

51CTO技術(shù)棧公眾號