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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 崔维友 中级黑马   /  2012-11-24 14:02  /  1673 人查看  /  3 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

本帖最后由 崔维友 于 2012-11-26 08:53 编辑

        通过对比较器Comparator的compare方法重写,我们可以很方便的按照自己的意愿对元素进行排序。但是如果作为键的比较内容是汉字,可能不会得到我们想要的结果,因为Comparable中的compareTo方法比较汉字时用的特定编码值。
        不过Java还提供了Collator类,它实现Comparator接口,执行区分语言环境的String比较,与其他区分语言环境的类一样,可以使用静态方法getInstance来为给定的语言环境获得适当的Collator对象。
        还有CollationKey类,表示遵守特定Collator对象规则的String。比较两个CollationKey将返回它们所表示的String的相对顺序。使用CollationKey来比较String通常比使用Collator.compare更快。不能直接创建CollationKey。而是通过调用Collator.getCollationKey来生成。只能比较同一个Collator对象生成的CollationKey。
        代码示例:
  1. /*
  2. 学生:姓名、年龄,地址。
  3. 姓名和年龄相同的视为同一人。
  4. */

  5. import java.text.*;
  6. import java.util.*;
  7. import java.util.Locale.*;

  8. class Student
  9. {
  10.         private String name;
  11.         private int age;
  12.         Student(String name, int age)
  13.         {
  14.                 this.name=name;
  15.                 this.age=age;
  16.         }
  17.         public String getName(){
  18.                 return name;
  19.         }
  20.         public int getAge(){
  21.                 return age;
  22.         }
  23.         public String toString(){
  24.                 return name+"___"+age;
  25.         }
  26. }
  27. //汉字比较器
  28. class StuNameComp implements Comparator<Student>
  29. {
  30.         public int compare(Student stu1, Student stu2)
  31.         {
  32.                 String s1=stu1.getName();
  33.                 String s2=stu2.getName();

  34.                 //Collator类执行区分语言环境的String比较,与其他区分语言环境的类一样,可以使用静态方法getInstance来为给定的语言环境获得适当的Collator对象。
  35.                 Collator collator = Collator.getInstance(Locale.CHINA);
  36.                 //CollationKey表示遵守特定Collator对象规则的String。比较两个CollationKey将返回它们所表示的String的相对顺序。使用CollationKey来比较String通常比使用Collator.compare更快。
  37.                 //不能直接创建CollationKey。而是通过调用Collator.getCollationKey来生成。只能比较同一个Collator对象生成的CollationKey。
  38.                 CollationKey key1 = collator.getCollationKey(s1);
  39.                 CollationKey key2 = collator.getCollationKey(s2);
  40.                
  41.                 int num=key1.compareTo(key2);
  42.                 if (num==0)
  43.                 {
  44.                         return new Integer(stu1.getAge()).compareTo(new Integer(stu2.getAge()));
  45.                 }
  46.                 return num;
  47.         }
  48. }

  49. class MapTest
  50. {
  51.         public static void main(String[] args)
  52.         {
  53.                 TreeMap<Student, String> ts=new TreeMap<Student, String>(new StuNameComp());
  54.                 ts.put(new Student("周鑫",21),"高庄");
  55.                 ts.put(new Student("英雄",24),"南埠东");
  56.                 ts.put(new Student("赵伟",26),"北麻村");
  57.                 ts.put(new Student("崔强",24),"南埠东");
  58.                 ts.put(new Student("一晴",24),"南埠东");
  59.                 //ts.put(new Student("怡情",24),"南埠东");        //“怡”字在Collator比较中存在bug,总是排在最后
  60.                 //ts.put(new Student("怡清",24),"南埠东");
  61.                 ts.put(new Student("李凯",23),"付家庄");
  62.                 ts.put(new Student("周红",25),"高庄");

  63.                 System.out.println();

  64.                 Set<Map.Entry<Student, String>> entrySet=ts.entrySet();
  65.                 Iterator<Map.Entry<Student, String>> ime=entrySet.iterator();
  66.                 while (ime.hasNext())
  67.                 {
  68.                         Map.Entry<Student, String> me=ime.next();
  69.                         Student s=me.getKey();
  70.                         String addr=me.getValue();
  71.                         System.out.println(s.toString()+"..."+addr);
  72.                 }
  73.         }
  74. }
复制代码

--------------------------------------------------------------
        通过这个方法可以满足汉字排序的一般需要,但是存在一个bug,就是汉字“怡”,首字为怡时一定往后放;多个首字为怡的词语排序时,再按照后面的字排序。
       新手入门,才疏学浅,希望哪位有更好的方法的能够不吝赐教!

评分

参与人数 1技术分 +1 收起 理由
古银平 + 1 赞一个!

查看全部评分

3 个回复

正序浏览
刘菲 发表于 2012-11-25 22:06
看过了,你很认真,要加油啊

:handshake,过奖了。一起努力!
回复 使用道具 举报
看过了,你很认真,要加油啊
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马