Java TreeMap 升序|降序排列
- 摘要:importjava.util.Comparator;importjava.util.TreeMap;publicclassMain{publicstaticvoidmain(String[]args){TreeMap<Integer,Integer>map1=newTreeMap<Integer,Integer>();//默认的TreeMap升序排列TreeMap<Integer,Integer>map2=newTreeMap<Integer
- 标签:Map Java
mono, serif; font-size: 12px; margin-bottom: 1px !important; margin-left: 45px !important;" class="dp-j">
- import?java.util.Comparator;??
- import?java.util.TreeMap;??
- public?class?Main?{??
- ????public?static?void?main(String[]?args)?{??
- ????????TreeMap<Integer,Integer>?map1?=?new?TreeMap<Integer,Integer>();????
- ????????TreeMap<Integer,Integer>?map2=?new?TreeMap<Integer,Integer>(new?Comparator<Integer>(){??
- ??????????????
- ?
- ?
- ?
- ?
- ????
- ????????????public?int?compare(Integer?a,Integer?b){??
- ????????????????return?b-a;???????????
- ????????????}??
- ????????????});??
- ????????map2.put(1,2);??
- ????????map2.put(2,4);??
- ????????map2.put(7,?1);??
- ????????map2.put(5,2);??
- ????????System.out.println("Map2="+map2);????
- ??????????
- ????????map1.put(1,2);??
- ????????map1.put(2,4);??
- ????????map1.put(7,?1);??
- ????????map1.put(5,2);??
- ????????System.out.println("map1="+map1);??
- ????}??
- ??????
- ??????
- ??????
- ??????
- }??
?