作业:
选择某种Map集合保存学号从1到15的学员的学号(键)和姓名(值),学号用字符串表示,输入的时候要以学号乱序的方式存入Map集合,然后按照学号从大到小的顺序将Map集合中的元素输出打印。需要自定义Map集合的比较器Comparator,因字符串对象的大小比较是按字典序,而非对应的数值。

要求:必须使用Map集合的内部排序机制进行排序,不能在外部排序。


package JAVA1;
import java.util.*;  
public class Map {  
  
    public static void main(String[] args) {  
        TreeMap map = new TreeMap( new MyComparator());  
        map.put("003", "代1");  
        map.put("006", "代2");  
        map.put("009", "代");  
        map.put("011", "代4");  
        map.put("015", "代5");  
        map.put("013", "代6");  
        map.put("012", "代7");  
        map.put("014", "代8");  
        map.put("010", "代9");  
        map.put("008", "代10");  
        map.put("007", "代11");  
        map.put("002", "代12");  
        map.put("005", "代13");  
        map.put("004", "代14");  
        map.put("001", "代15");  
          
        Set keySet = map.keySet();  
        Iterator it = keySet.iterator();  
        while(it.hasNext())  
        {  
            Object key = it.next();  
            Object value = map.get(key);  
            System.out.println(key + ":"+value);  
        }  
    }  
  
}  
  
class MyComparator implements Comparator{  
    public int compare(Object obj1, Object obj2) {  
  
        String l1=(String)obj1;  
        String l2=(String)obj2;  
        return l1.compareTo(l2);  
    }  
  
}  选择某种Map集合保存学号从1到15的学员的学号

相关文章:

  • 2022-12-23
  • 2021-11-29
  • 2021-08-29
  • 2021-11-20
  • 2021-05-17
  • 2021-12-27
  • 2021-08-24
  • 2021-12-09
猜你喜欢
  • 2021-09-18
  • 2021-04-24
  • 2021-10-03
  • 2022-12-23
  • 2021-11-16
  • 2021-05-17
  • 2021-12-23
相关资源
相似解决方案