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

成都創(chuàng)新互聯(lián)網(wǎng)站建設(shè)公司,提供成都網(wǎng)站設(shè)計(jì)、做網(wǎng)站,網(wǎng)頁(yè)設(shè)計(jì),建網(wǎng)站,PHP網(wǎng)站建設(shè)等專(zhuān)業(yè)做網(wǎng)站服務(wù);可快速的進(jìn)行網(wǎng)站開(kāi)發(fā)網(wǎng)頁(yè)制作和功能擴(kuò)展;專(zhuān)業(yè)做搜索引擎喜愛(ài)的網(wǎng)站,是專(zhuān)業(yè)的做網(wǎng)站團(tuán)隊(duì),希望更多企業(yè)前來(lái)合作!
1. HashMap存儲(chǔ)每對(duì)鍵和值作為一個(gè)Entry
- Map
aMap = new HashMap ();
鍵的每次插入,都會(huì)有值對(duì)應(yīng)到散列映射上,生成一個(gè)Entry
2.創(chuàng)建一個(gè)簡(jiǎn)單的HashMap,并插入一些鍵和值。
- ap
aMap = new HashMap (); - // adding keys and values
- aMap.put("Five", 5);
- aMap.put("Seven", 7);
- aMap.put("Eight", 8);
- aMap.put("One",1);
- aMap.put("Two",2);
- aMap.put("Three", 3);
3.從HashMap恢復(fù)entry集合,如下所示。
- Set
> mapEntries = aMap.entrySet();
4.從上述mapEntries創(chuàng)建LinkedList。我們將排序這個(gè)鏈表來(lái)解決順序問(wèn)題。我們之所以要使用鏈表來(lái)實(shí)現(xiàn)這個(gè)目的,是因?yàn)樵阪湵碇胁迦朐乇葦?shù)組列表更快。
- List
> aList = new LinkedList >(mapEntries);
5.通過(guò)傳遞鏈表和自定義比較器來(lái)使用Collections.sort()方法排序鏈表。
- Collections.sort(aList, new Comparator
>() { - @Override
- public int compare(Entry
ele1, - Entry
ele2) { - return ele1.getValue().compareTo(ele2.getValue());
- }
- });
6.使用自定義比較器,基于entry的值(Entry.getValue()),來(lái)排序鏈表。
7. ele1.getValue(). compareTo(ele2.getValue())——比較這兩個(gè)值,返回0——如果這兩個(gè)值完全相同的話;返回1——如果***個(gè)值大于第二個(gè)值;返回-1——如果***個(gè)值小于第二個(gè)值。
8. Collections.sort()是一個(gè)內(nèi)置方法,僅排序值的列表。它在Collections類(lèi)中重載。這兩種個(gè)方法是
- public static
> void sort(List list) - public static
void sort(List list, Comparator super T> c)
9.現(xiàn)在你已經(jīng)排序鏈表,我們需要存儲(chǔ)鍵和值信息對(duì)到新的映射中。由于HashMap不保持順序,因此我們要使用LinkedHashMap。
- // Storing the list into Linked HashMap to preserve the order of insertion.
- Map
aMap2 = new LinkedHashMap (); - for(Entry
entry: aList) { - aMap2.put(entry.getKey(), entry.getValue());
- }
10.完整的代碼如下。
- package com.speakingcs.maps;
- import java.util.Collections;
- import java.util.Comparator;
- import java.util.HashMap;
- import java.util.LinkedHashMap;
- import java.util.LinkedList;
- import java.util.List;
- import java.util.Map;
- import java.util.Map.Entry;
- import java.util.Set;
- public class SortMapByValues {
- public static void main(String[] args) {
- Map
aMap = new HashMap (); - // adding keys and values
- aMap.put("Five", 5);
- aMap.put("Seven", 7);
- aMap.put("Eight", 8);
- aMap.put("One",1);
- aMap.put("Two",2);
- aMap.put("Three", 3);
- sortMapByValues(aMap);
- }
- private static void sortMapByValues(Map
aMap) { - Set
> mapEntries = aMap.entrySet(); - System.out.println("Values and Keys before sorting ");
- for(Entry
entry : mapEntries) { - System.out.println(entry.getValue() + " - "+ entry.getKey());
- }
- // used linked list to sort, because insertion of elements in linked list is faster than an array list.
- List
> aList = new LinkedList >(mapEntries); - // sorting the List
- Collections.sort(aList, new Comparator
>() { - @Override
- public int compare(Entry
ele1, - Entry
ele2) { - return ele1.getValue().compareTo(ele2.getValue());
- }
- });
- // Storing the list into Linked HashMap to preserve the order of insertion.
- Map
aMap2 = new LinkedHashMap (); - for(Entry
entry: aList) { - aMap2.put(entry.getKey(), entry.getValue());
- }
- // printing values after soring of map
- System.out.println("Value " + " - " + "Key");
- for(Entry
entry : aMap2.entrySet()) { - System.out.println(entry.getValue() + " - " + entry.getKey());
- }
- }
- }
譯文鏈接:http://www.codeceo.com/article/java-hashmap-value-sort.html
英文原文:How to Sort HashMap Based On Values in Java
文章標(biāo)題:Java面試題:如何對(duì)HashMap按鍵值排序
新聞來(lái)源:http://fisionsoft.com.cn/article/dhgjpjh.html


咨詢(xún)
建站咨詢(xún)
