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);
	}
}

 

相关文章:

  • 2021-09-26
  • 2022-12-23
  • 2021-08-22
  • 2022-12-23
  • 2021-09-24
  • 2021-11-17
猜你喜欢
  • 2021-06-12
  • 2021-10-04
  • 2021-08-09
  • 2022-12-23
  • 2022-01-04
  • 2021-12-15
  • 2022-12-23
相关资源
相似解决方案