在對Java無序類集合,如List(ArrayList/LinkedList)、HashSet(TreeSet有序)、HashMap等排序時,Java中一個公共的類Collections,提供了對Java集合排序等很好的方法sort。 可是有一個要求是sort方法的參數爲<List list> 或<List list, Comparator<? super T> c>,即排序對象要求必須是List類型。java
sort 方法的參數必須爲List 的緣由是,只有List能夠定義排序的方法,讓List中的元素改變在構建List時原始的相對位置(初始構建時,元素相對位置即爲元素初始加入順序)。HashSet、HashMap 在構建時,初始加入的元素已經按照元素的hashCode()方法的定義排好序。因此這裏所說的HashSet 排序 和 HashMap 排序是指:將其中的元素導出到另外一個集合中,對該載體集合排序。排序以後,原HashSet 和 HashMap 中元素順序沒有變。函數
故而對Java無序類集合的排序問題,基本思路就是:將HashSet 或 HashMap 中的元素取出放入 List 中,對List 用 Collections.sort() 方法排序,以後輸出排序後List中的元素,即爲對Set/Map 中元素排序後的結果。注意HashSet、HashMap 中元素位置沒有改變,依然只和 初始構建時,元素自己自定義的hashCode() 方法有關。spa
import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; import java.util.HashMap; import java.util.HashSet; import java.util.Iterator; import java.util.LinkedList; import java.util.Map; public class Test { public static void main(String[] args){ ArrayList<String> listTest = new ArrayList<String>(); listTest.add("bbc"); listTest.add("abc"); listTest.add("acb"); HashSet<String> setTest = new HashSet<String>(); setTest.add("bbc"); setTest.add("abc"); setTest.add("acb"); System.out.println("HashSet BeforeSort:"); for(String s : setTest) System.out.println(s); HashMap<String, Integer> mapTest = new HashMap<String, Integer>(); mapTest.put("bbc", 1); mapTest.put("abc", 2); mapTest.put("acb", 3); System.out.println("HashMap BeforeSort:"); for(Map.Entry<String, Integer> entry : mapTest.entrySet()) System.out.println(entry.getKey() + " " + entry.getValue()); /* * List */ Collections.sort(listTest); Iterator<String> list_iter = listTest.iterator(); while(list_iter.hasNext()) System.out.println(list_iter.next()); /* * Set */ LinkedList<String> setSort = new LinkedList<String>(setTest); //Collections.sort(setSort); Comparator<String> setComp = Collections.reverseOrder(); Collections.sort(setSort, setComp); /*LinkedList<String> setSort = new LinkedList<String>(); for(String s : setTest) setSort.add(s);*/ for(String s : setTest) System.out.println(s); for(String s : setSort) System.out.println(s); /* * Map */ LinkedList<String> mapSort = new LinkedList<String>(); mapSort.addAll(mapTest.keySet()); //Collections.sort(mapSort); Comparator<String> mapComp = Collections.reverseOrder(); Collections.sort(mapSort, mapComp); for(Map.Entry<String, Integer> entry : mapTest.entrySet()) System.out.println(entry.getKey() + " " + entry.getValue()); for(final Iterator<String> map_iter= mapSort.iterator(); map_iter.hasNext();) System.out.println(map_iter.next()); /* LinkedList<Map.Entry<String, Integer>> mapEntry = new LinkedList<Map.Entry<String,Integer>>(); mapEntry.addAll(mapTest.entrySet()); Collections.sort(mapEntry, new Comparator<Map.Entry<String, Integer>>() { public int compare(Map.Entry<String, Integer> a, Map.Entry<String, Integer> b){ if(a.getValue() > b.getValue()) return -1; else return 1; } }); for(Map.Entry<String, Integer> entry : mapEntry) System.out.println(entry.getKey() + " " +entry.getValue()); for(Map.Entry<String, Integer> entry : mapTest.entrySet()) System.out.println(entry.getKey() + " " + entry.getValue());*/ } }
HashSet BeforeSort: abc acb bbc HashMap BeforeSort: abc 2 acb 3 bbc 1 //List AfterSort abc acb bbc //HashSet AfterSort abc acb bbc //setSort AfterSort (setSort is means HashSet to LinkedList) bbc acb abc //HashMap AfterSort abc 2 acb 3 bbc 1 //mapSort AfterSort (mapSort is means HashMap to LinkedList) bbc acb abc
1、按key值排序 假設HashMap存儲的鍵-值對爲(String,Integer),按key排序能夠調用JDK函數sort(默認的按字典升序): Set<String> keySet = map.keySet(); Collections.sort(keySet); for(Iterator<String> ite = keySet.iterator(); ite.hasNext();) { String temp = ite.next(); System.out.println("key-value: "+temp+","+map.getValue(temp); } 若是想要按字典的降序排列,則需改寫sort方法裏面的比較器Comparator: Collections.sort(keySet, new Comparator() { public int compare(Object o1, Object o2) { if(Integer.parseInt(o1.toString())>Integer.parseInt(o2.toString()) return 1; if(Integer.parseInt(o1.toString())==Integer.parseInt(o2.toString()) return 0; else return -1; } }); 2、按value值排序 1)方法一:用兩個list鏈表實現 List<String> keyList = new LinkedList<String>(); keyList.addAll(map.keySet()); List<Integer> valueList = new LinkedList<Integer>(); valueList.addAll(map.values()); for(int i=0; i<valueList.size(); i++) for(int j=i+1; j<valueList.size(); j++) { if(valueList.get(j)>valueList.get(i)) { valueList.set(j, valueList.get(i)); valueList.set(i, valueList.get(j)); //一樣調整對應的key值 keyList.set(j, keyList.get(i)); keyList.set(i, kyeList.get(j)); } 而後依次把key值和對應value值從新裝入HashMap便可。 2)方法二:改寫JDK提供的Comparator接口方法compare List<Map.Entry<String, Integer>> list = new LinkedList<Map.Entry<String, Integer>>(); list.addAll(map.entrySet()); Collections.sort(list, new Comparator<Map.Entry<String, Integer>>() { public int compare(Map.Entry obj1, Map.Entry obj2) {//從高往低排序 if(Integer.parseInt(obj1.getValue().toString())<Integer.parseInt(obj2.getValue().toString())) return 1; if(Integer.parseInt(obj1.getValue().toString())==Integer.parseInt(obj2.getValue().toString())) return 0; else return -1; } }); for(Iterator<Map.Entry<String, Integer>> ite = list.iterator(); ite.hasNext();) { Map.Entry<String, Integer> map = ite.next(); System.out.println("key-value: " + map.getKey() + "," + map.getValue()); }