Map 这个在java 程序中最常见的类型,通常使用的是其具体的实现 hashmap 来定义数据。但存储在hashmap里面的数据,经常会根据需要,进行排序处理,在这里总结了两种方法来排序,如果以后发现其他的,继续增加
1. 根据 map 的 key 值来排序 ( 利用treemap 特性实现 )
下面是两种处理办法的代码
运行之后,你会发现,输出结果按照 key 的顺序排列了,说明这种方法可行.
1. 根据 map 的 key 值来排序 ( 利用treemap 特性实现 )
2. 根据 map 的 value 值来排序( 利用 list 特性实现 )
3. 根据 map 的 key / value 值来排序(将map转化为二维数组进行排序)
下面是两种处理办法的代码
第一种办法,根据key 排序
package test;
import java.util.HashMap;
import java.util.Map;
import java.util.TreeMap;
public class MapOrderByKey {
public static void main(String[] args) {
Map<String, String> unsortMap = new HashMap<String, String>();
unsortMap.put("2", "B");
unsortMap.put("1", "A");
unsortMap.put("4", "D");
unsortMap.put("3", "B");
unsortMap.put("7", "C");
unsortMap.put("5", "z");
unsortMap.put("6", "b");
unsortMap.put("8", "a");
System.out.println("Unsort Map......");
printMap(unsortMap);
System.out.println("Sorted Map......");
Map<String, String> treeMap = new TreeMap<String, String>(unsortMap);
printMap(treeMap);
}
public static void printMap(Map<String, String> map) {
for (Map.Entry entry : map.entrySet()) {
System.out.println("Key : " + entry.getKey() + " Value : "
+ entry.getValue());
}
}
}
运行之后,你会发现,输出结果按照 key 的顺序排列了,说明这种方法可行.
结果如下:
Unsort Map......
Key : 3 Value : B
Key : 2 Value : B
Key : 1 Value : A
Key : 7 Value : C
Key : 6 Value : b
Key : 5 Value : z
Key : 4 Value : D
Key : 8 Value : a
Sorted Map......
Key : 1 Value : A
Key : 2 Value : B
Key : 3 Value : B
Key : 4 Value : D
Key : 5 Value : z
Key : 6 Value : b
Key : 7 Value : C
Key : 8 Value : a
第二种办法,根据value 进行排序
package test;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
public class MapOrderByValue {
public static void main(String[] args) {
Map<String, String> unsortMap = new HashMap<String, String>();
unsortMap.put("2", "B");
unsortMap.put("1", "A");
unsortMap.put("4", "D");
unsortMap.put("3", "B");
unsortMap.put("7", "C");
unsortMap.put("5", "z");
unsortMap.put("6", "b");
unsortMap.put("8", "a");
System.out.println("Unsort Map......");
printMap(unsortMap);
System.out.println("Sorted Map......");
Map<String, String> sortedMap = sortByComparator(unsortMap);
printMap(sortedMap);
}
private static Map sortByComparator(Map unsortMap) {
List list = new LinkedList(unsortMap.entrySet());
// sort list based on comparator
Collections.sort(list, new Comparator() {
public int compare(Object o1, Object o2) {
return ((Comparable) ((Map.Entry) (o1)).getValue())
.compareTo(((Map.Entry) (o2)).getValue());
}
});
// put sorted list into map again
//LinkedHashMap make sure order in which keys were inserted
Map sortedMap = new LinkedHashMap();
for (Iterator it = list.iterator(); it.hasNext();) {
Map.Entry entry = (Map.Entry) it.next();
sortedMap.put(entry.getKey(), entry.getValue());
}
return sortedMap;
}
public static void printMap(Map<String, String> map){
for (Map.Entry entry : map.entrySet()) {
System.out.println("Key : " + entry.getKey()
+ " Value : " + entry.getValue());
}
}
}
运行,你会发现,后来的结果是按照value来排序的。在仔细看代码,你会发现,其实现的一个基本原理就是将 map 转为 list ,然后利用Comparator 对 list 进行排序,最后再把排好序的值放入 map 里面。这样就实现了在map里面对value 进行排序.
第三种方法:
1、首先将map转化为二维数组
2、在进行二维数组排序
/**
* 二维数组排序
* @param sortArrays s
* @param sortIndex
* @param Sequence {desc,asc}
*/
public static void sort(int[][] s,int sortIndex,String sequence) {
for (int j = 0; j < s.length ; j++) {
for (int i = 0; i < s.length - 1; i++) {
int[] ss;
if("desc".equals(sequence.trim())){
if (s[i][sortIndex]-(s[i + 1][sortIndex]) < 0) {
ss = s[i];
s[i] = s[i + 1];
s[i + 1] = ss;
}
}else if("asc".equals(sequence.trim())){
if (s[i][sortIndex]-(s[i + 1][sortIndex]) > 0) {
ss = s[i];
s[i] = s[i + 1];
s[i + 1] = ss;
}
}
}
}
}