本篇文章給大家分享的是有關(guān)Java中怎么對HashMap按鍵值排序,小編覺得挺實用的,因此分享給大家學(xué)習(xí),希望大家閱讀完這篇文章后可以有所收獲,話不多說,跟著小編一起來看看吧。
成都創(chuàng)新互聯(lián)公司2013年成立,先為勉縣等服務(wù)建站,勉縣等地企業(yè),進行企業(yè)商務(wù)咨詢服務(wù)。為勉縣企業(yè)網(wǎng)站制作PC+手機+微官網(wǎng)三網(wǎng)同步一站式服務(wù)解決您的所有建站問題。
1. HashMap存儲每對鍵和值作為一個Entry
MapaMap = new HashMap ();
鍵的每次插入,都會有值對應(yīng)到散列映射上,生成一個Entry
2.創(chuàng)建一個簡單的HashMap,并插入一些鍵和值。
apaMap = 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。我們將排序這個鏈表來解決順序問題。我們之所以要使用鏈表來實現(xiàn)這個目的,是因為在鏈表中插入元素比數(shù)組列表更快。
List> aList = new LinkedList >(mapEntries);
5.通過傳遞鏈表和自定義比較器來使用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()),來排序鏈表。
7. ele1.getValue(). compareTo(ele2.getValue())——比較這兩個值,返回0——如果這兩個值完全相同的話;返回1——如果***個值大于第二個值;返回-1——如果***個值小于第二個值。
8. Collections.sort()是一個內(nèi)置方法,僅排序值的列表。它在Collections類中重載。這兩種個方法是
public static> void sort(List list) public static void sort(List list, Comparator super T> c)
9.現(xiàn)在你已經(jīng)排序鏈表,我們需要存儲鍵和值信息對到新的映射中。由于HashMap不保持順序,因此我們要使用LinkedHashMap。
// Storing the list into Linked HashMap to preserve the order of insertion. MapaMap2 = 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) { MapaMap = 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()); } } }
以上就是Java中怎么對HashMap按鍵值排序,小編相信有部分知識點可能是我們?nèi)粘9ぷ鲿姷交蛴玫降?。希望你能通過這篇文章學(xué)到更多知識。更多詳情敬請關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。