A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© qihuan 中级黑马   /  2015-7-11 23:28  /  647 人查看  /  0 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

本帖最后由 qihuan 于 2015-7-12 07:38 编辑
  1. package practice;

  2. import java.util.*;
  3. /**
  4. * 需求:
  5. * 1.对学生对象的年龄进行升序排序。
  6. * 2.对学生对象的姓名进行升序排序。
  7. * 使用可以排序的Map集合-TreeMap
  8. * @author Qihuan
  9. *
  10. */

  11. //姓名比较器
  12. class NameComparator implements Comparator<Student> {
  13.         public int compare(Student s1, Student s2){
  14.                 int num = s1.getName().compareTo(s2.getName());
  15.                 if(num == 0)
  16.                         return new Integer(s1.getAge()).compareTo(new Integer(s2.getAge()));
  17.                 return num;
  18.         }
  19. }

  20. public class TreeMapTest {
  21.         public static void main(String[] args) {
  22.                 TreeMap<Student,String> tm = new TreeMap<Student, String>(new NameComparator());
  23.                
  24.                 tm.put(new Student("Lily", 17), "tianjin");
  25.                 tm.put(new Student("Bob", 12), "shanghai");
  26.                 tm.put(new Student("Tom", 29), "shanghai");
  27.                 tm.put(new Student("Tang", 23), "guangzhou");
  28.                 tm.put(new Student("Aby", 34), "hainan");
  29.                
  30.                 Set<Map.Entry<Student, String>> entrySet = tm.entrySet();
  31.                
  32.                 for(Iterator<Map.Entry<Student, String>> it = entrySet.iterator();it.hasNext();){
  33.                         Map.Entry<Student, String> me = it.next();
  34.                         
  35.                         Student s = me.getKey();
  36.                         String addr = me.getValue();
  37.                         
  38.                         System.out.println(s.getName()+"\t"+s.getAge()+"\t"+addr);
  39.                 }
  40.         }
  41. }
复制代码
  1. class Student implements Comparable<Student>{
  2.         //定义姓名、nianl
  3.         private String name;
  4.         private int age;
  5.         //定义方法,传入姓名年龄
  6.         public Student(String name, int age) {
  7.                 // TODO Auto-generated constructor stub
  8.                 this.name = name;
  9.                 this.age = age;
  10.         }
  11.         //获取姓名年龄
  12.         public String getName(){
  13.                 return name;
  14.         }
  15.         public int getAge(){
  16.                 return age;
  17.         }
  18.        
  19.         //重写equals方法
  20.         public boolean equals(Object obj){
  21.                 if(!(obj instanceof Student))
  22.                         throw new ClassCastException("类型不匹配!");
  23.                 Student s = (Student)obj;
  24.                 return this.name.equals(s.name) && this.age==s.age;
  25.         }
  26.        
  27.         public int hashCode() {
  28.                 return name.hashCode()+age;
  29.         }
  30.         @Override
  31.         public int compareTo(Student s) {
  32.                 // TODO Auto-generated method stub
  33.                 int num = new Integer(this.age).compareTo(new Integer(s.age));
  34.                 if(num==0)
  35.                         return this.name.compareTo(s.name);
  36.                 return num;
  37.         }
  38. }
复制代码



0 个回复

您需要登录后才可以回帖 登录 | 加入黑马