日韩无码专区无码一级三级片|91人人爱网站中日韩无码电影|厨房大战丰满熟妇|AV高清无码在线免费观看|另类AV日韩少妇熟女|中文日本大黄一级黄色片|色情在线视频免费|亚洲成人特黄a片|黄片wwwav色图欧美|欧亚乱色一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
Java面試題:如何對HashMap按鍵值排序

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

成都創(chuàng)新互聯(lián)網(wǎng)站建設(shè)公司,提供成都網(wǎng)站設(shè)計、做網(wǎng)站,網(wǎng)頁設(shè)計,建網(wǎng)站,PHP網(wǎng)站建設(shè)等專業(yè)做網(wǎng)站服務(wù);可快速的進行網(wǎng)站開發(fā)網(wǎng)頁制作和功能擴展;專業(yè)做搜索引擎喜愛的網(wǎng)站,是專業(yè)的做網(wǎng)站團隊,希望更多企業(yè)前來合作!

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

 
 
  1. Map aMap = new HashMap(); 

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

2.創(chuàng)建一個簡單的HashMap,并插入一些鍵和值。

 
 
  1. ap aMap = new HashMap(); 
  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> mapEntries = aMap.entrySet(); 

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

 
 
  1. List> aList = new LinkedList>(mapEntries); 

5.通過傳遞鏈表和自定義比較器來使用Collections.sort()方法排序鏈表。

 
 
  1. Collections.sort(aList, new Comparator>() { 
  2.  
  3.             @Override 
  4.  
  5.             public int compare(Entry ele1, 
  6.  
  7.                     Entry 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 > void sort(List list) 
  2.  
  3. public static  void sort(List list, Comparator c) 

9.現(xiàn)在你已經(jīng)排序鏈表,我們需要存儲鍵和值信息對到新的映射中。由于HashMap不保持順序,因此我們要使用LinkedHashMap。

 
 
  1. // Storing the list into Linked HashMap to preserve the order of insertion.      
  2. Map aMap2 = new LinkedHashMap(); 
  3.         for(Entry 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 aMap = new HashMap(); 
  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 aMap) { 
  32.  
  33.         Set> mapEntries = aMap.entrySet(); 
  34.  
  35.         System.out.println("Values and Keys before sorting "); 
  36.         for(Entry 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> aList = new LinkedList>(mapEntries); 
  42.  
  43.         // sorting the List 
  44.         Collections.sort(aList, new Comparator>() { 
  45.  
  46.             @Override 
  47.             public int compare(Entry ele1, 
  48.                     Entry 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 aMap2 = new LinkedHashMap(); 
  56.         for(Entry 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 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


本文標題:Java面試題:如何對HashMap按鍵值排序
URL分享:http://m.5511xx.com/article/dhgjpjh.html