参考:
http://blog.csdn.net/xiaoyu714543065/article/details/38519817
http://blog.csdn.net/lishengbo/article/details/6193509
##########################################################
降序操作:按key降序
import java.util.Comparator; import java.util.Iterator; import java.util.Map; import java.util.Set; import java.util.TreeMap; public class test { public static void main(String[] args) { Map<Double, String> map = new TreeMap<Double, String>( new Comparator<Double>() { public int compare(Double obj1, Double obj2) { // 降序排序 return obj2.compareTo(obj1); } }); map.put(2.33, "ccccc"); map.put(2.0, "aaaaa"); map.put(3.0, "bbbbb"); map.put(4.0, "ddddd"); Set<Double> keySet = map.keySet(); Iterator<Double> iter = keySet.iterator(); while (iter.hasNext()) { Double key = iter.next(); System.out.println(key + ":" + map.get(key)); } } }
TreeMap默认是升序的,如果我们需要改变排序方式,则需要使用比较器:Comparator。Comparator可以对集合对象或者数组进行排序的比较器接口,实现该接口的public compare(T o1,To2)
方法即可实现排序,
import java.util.Comparator;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
public class TreeMapTest {
public static void main(String[] args) {
Map<String, String> map = new TreeMap<String, String>(
new Comparator<String>() {
public int compare(String obj1, String obj2) {
// 降序排序
return obj2.compareTo(obj1);
}
});
map.put("b", "ccccc");
map.put("d", "aaaaa");
map.put("c", "bbbbb");
map.put("a", "ddddd");
Set<String> keySet = map.keySet();
Iterator<String> iter = keySet.iterator();
while (iter.hasNext()) {
String key = iter.next();
System.out.println(key + ":" + map.get(key));
}
}
}
按value:
上面例子是对根据TreeMap的key值来进行排序的,但是有时我们需要根据TreeMap的value来进行排序。对value排序我们就需要借助于Collections的sort(List<T> list, Comparator<? super T> c)方法,该方法根据指定比较器产生的顺序对指定列表进行排序。但是有一个前提条件,那就是所有的元素都必须能够根据所提供的比较器来进行比较
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 TreeMapTest {
public static void main(String[] args) {
Map<String, String> map = new TreeMap<String, String>();
map.put("a", "ddddd");
map.put("c", "bbbbb");
map.put("d", "aaaaa");
map.put("b", "ccccc");
//这里将map.entrySet()转换成list
List<Map.Entry<String,String>> list = new ArrayList<Map.Entry<String,String>>(map.entrySet());
//然后通过比较器来实现排序
Collections.sort(list,new Comparator<Map.Entry<String,String>>() {
//升序排序
public int compare(Entry<String, String> o1,
Entry<String, String> o2) {
return o1.getValue().compareTo(o2.getValue());
}
});
for(Map.Entry<String,String> mapping:list){
System.out.println(mapping.getKey()+":"+mapping.getValue());
}
}
}
###################################################
升序排序:按key
import java.util.Map.Entry; import java.util.*; public class test { public static void main(String[] args) { HashMap<String,String> map=new HashMap<String, String>(); map.put("1","11"); map.put("2", "22"); map.put("3", "33"); for (Entry<String,String> entry: map.entrySet()) { System.out.println("排序之前:"+entry.getKey()+" 值"+entry.getValue()); } System.out.println("======================================================"); SortedMap<String,String> sort=new TreeMap<String,String>(map); Set<Entry<String,String>> entry1=sort.entrySet(); Iterator<Entry<String,String>> it=entry1.iterator(); while(it.hasNext()) { Entry<String,String> entry=it.next(); System.out.println("排序之后:"+entry.getKey()+" 值"+entry.getValue()); } } }