您的位置:首页 > 其它

对map的值进行排序

2015-06-27 12:07 423 查看
package com.asp;

import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.SortedMap;
import java.util.TreeMap;

public class MapUtil {
/**
* 按map的value升序排序
*
* @param map
* @param top
*
* @return
*/
public static <K, V extends Comparable<? super V>> Map<K, V> sortByValueAsc(
Map<K, V> map, int top) {
List<Map.Entry<K, V>> list = new LinkedList<Map.Entry<K, V>>(
map.entrySet());
Collections.sort(list, new Comparator<Map.Entry<K, V>>() {
public int compare(Map.Entry<K, V> o1, Map.Entry<K, V> o2) {
return (o1.getValue()).compareTo(o2.getValue());
}
});

Map<K, V> result = new LinkedHashMap<K, V>();
for (Map.Entry<K, V> entry : list) {
if (top-- == 0) {
break;
}
result.put(entry.getKey(), entry.getValue());
}
return result;
}

/**
* 按map value降序排序
*
* @param map
* @param top
*
* @return
*/
public static <K, V extends Comparable<? super V>> Map<K, V> sortByValueDesc(
Map<K, V> map, int top) {
List<Map.Entry<K, V>> list = new LinkedList<Map.Entry<K, V>>(
map.entrySet());
Collections.sort(list, new Comparator<Map.Entry<K, V>>() {
public int compare(Map.Entry<K, V> o1, Map.Entry<K, V> o2) {
return (o2.getValue()).compareTo(o1.getValue());
}
});

Map<K, V> result = new LinkedHashMap<K, V>();
for (Map.Entry<K, V> entry : list) {
if (top-- == 0) {
break;
}
result.put(entry.getKey(), entry.getValue());
}
return result;
}

/**
* 按key排序
*
* @param unsort_map
* @return
*/
public static SortedMap<String, Object> mapSortByKey(
Map<String, Object> unsort_map) {
TreeMap<String, Object> result = new TreeMap<String, Object>();

Object[] unsort_key = unsort_map.keySet().toArray();
Arrays.sort(unsort_key);

for (int i = 0; i < unsort_key.length; i++) {
result.put(unsort_key[i].toString(), unsort_map.get(unsort_key[i]));
}
return result.tailMap(result.firstKey());
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: