|
#1 |
|
|
#2 |
|
|
#3 |
回复1楼: = =能不能详细点,而且我需要的只是取出前十个 |
|
30分
#4 |
package Map;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.TreeMap;
public class SortMap {
public static void main(String[] args) {
Map map=new TreeMap ();
map.put("图书" , 4);
map.put("音像" , 6);
map.put("素材" , 9);
map.put("音乐" , 8);
map.put("影视" , 7);
map.put("动漫" , 4);
map.put("歌曲" , 3);
map.put("图片" , 2);
map.put("图标" , 6);
ArrayList<Map.Entry<String,Integer>> entries= sortMap(map);
for( int i=0;i<5;i++){
System. out.print(entries.get(i).getKey()+":" +entries.get(i).getValue());
}
}
public static ArrayList<Map.Entry<String,Integer>> sortMap(Map map){
List<Map.Entry<String, Integer>> entries = new ArrayList<Map.Entry<String, Integer>>(map.entrySet());
Collections.sort(entries, new Comparator<Map.Entry<String, Integer>>() {
public int compare(Map.Entry<String, Integer> obj1 , Map.Entry<String, Integer> obj2) {
return obj2.getValue() - obj1.getValue();
}
});
return (ArrayList<Entry<String, Integer>>) entries;
}
}
|
|
#5 |
回复4楼: 小屌丝改成小萝莉…我感觉怪怪的… |
|
#6 |
回复4楼: 太棒了,就是这个,谢谢 |