TreeMap的应用

public class SortedMap
{
	//treemap按key排序,默认是升序,可自定义降序
	public static void main(String[] args) 
	{
		Map<Integer, String> map = new TreeMap<>(new Comparator<Integer>()
				{
					public int compare(Integer a, Integer b)
					{
						//return a < b ? 1 : (a == b)? 0 : -1;
						return b.compareTo(a);
					}
				}
		);
		map.put(5, "java");
		map.put(2, "c");
		map.put(6, "jsp");
		System.out.println(map);
	}
}

 

posted @ 2016-10-17 10:33  32ddd  阅读(325)  评论(0编辑  收藏  举报