Collections.sort自定義排序的使用方法


總結:Collections可以對List進行排序;如果想對Map進行排序,可以將Map轉化成List,進行排序;

 

	public static void hashMapSortTest() {
		 Map<String, Integer> maps = new HashMap<String, Integer>();   
		 maps.put("boy", 8);   
		 maps.put("cat", 7);   
		 maps.put("dog", 1);   
		 maps.put("apple", 5);   

		 Iterator i = maps.entrySet().iterator();   
		 while (i.hasNext()) {   
		     Map.Entry<String, Integer> entry1 = (Map.Entry<String, Integer>) i.next();   
		 }   
		 List<Map.Entry<String, Integer>> info = new ArrayList<Map.Entry<String, Integer>>(maps.entrySet());   
		 Collections.sort(info, new Comparator<Map.Entry<String, Integer>>() {   
		     public int compare(Map.Entry<String, Integer> obj1, Map.Entry<String, Integer> obj2) {   
		         return obj1.getValue().compareTo(obj2.getValue()); 
		     }   
		 });   		
	}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM