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