JAVA中對TreeMap中文排序
阿新 • • 發佈:2019-01-26
專案中用到一個對於Map排序的。下面貼出程式碼:
package org.hsyd.test;
import java.text.CollationKey;
import java.text.Collator;
import java.util.Comparator;
/**
* 對treeMap排序
* @author Administrator
*
*/
public class CollatorComparator implements Comparator {
Collator collator = Collator.getInstance();
public int compare(Object element1, Object element2) {
CollationKey key1 = collator.getCollationKey(element1.toString());
CollationKey key2 = collator.getCollationKey(element2.toString());
return key1.compareTo(key2);
}
}
//這樣引用:
CollatorComparator comparator = new CollatorComparator();
TreeMap<String, HashMap<Long, Integer>> map=new TreeMap<String, HashMap<Long,Integer>>(comparator);