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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© wudongzhe 中级黑马   /  2013-4-30 18:56  /  1510 人查看  /  9 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

本帖最后由 吴东泽 于 2013-5-1 10:47 编辑

就是让集合具备比较功能 覆盖 Comparator接口  中的compare方法   但是怎么覆盖不会了。  之前看视频都是对存入的对象进行排序                    
比如要对这里面的数字排序 如果数字相同在按字符串排序  TreeMap<String,Integer> tm=new TreeMap<String,Integer>();

能给我个具体覆盖代码最好,谢啦

评分

参与人数 1技术分 +1 收起 理由
黄玉昆 + 1

查看全部评分

9 个回复

倒序浏览

import java.util.*;

class Comp implements Comparator<String>//定义比较器
{
        public int compare(String a,String b)//覆盖比较方法
        {
                return b.compareTo(a);
        }
}

class Demo
{
        public static void main(String[] args)
        {
                //不用比较器的话,就用String默认的比较方式,因为String实现了Comparable接口
                //TreeMap<String,Integer> tm = new TreeMap<String,Integer>();
                TreeMap<String,Integer> tm = new TreeMap<String,Integer>(new Comp());
                tm.put("11",1);
                tm.put("111",2);
                tm.put("1",3);

                for(Iterator<String> it = tm.keySet().iterator();it.hasNext();)//遍历集合元素
                {
                        String key = it.next();
                        Integer value = tm.get(key);
                        System.out.println(key+"..."+value);
                }
        }
}

评分

参与人数 1技术分 +1 收起 理由
黄玉昆 + 1

查看全部评分

回复 使用道具 举报
我不是想要字符串排序啊  我想要数字排序。。
回复 使用道具 举报
肯定是做到入学测试题了吧
回复 使用道具 举报
不是入学测试。。。。已经面试过了。
回复 使用道具 举报
哥们,这个程序先按数字(年龄)排序,数字相同的话按照字符串(姓名)排序,为学生排序的

回复 使用道具 举报
  1. import java.util.*;
  2. class Student implements Comparable<Student>
  3. {
  4.         private String name;
  5.         private int age;
  6.         Student(String name,int age)
  7.         {
  8.                 this.name = name;
  9.                 this.age = age;
  10.         }
  11.         public int compareTo(Student s)
  12.         {
  13.                 int num = new Integer(this.age).compareTo(new Integer(s.age));
  14.                 if(num==0)
  15.                         return this.name.compareTo(s.name);;
  16.                 return num;
  17.         }
  18.         public int hashCode()
  19.         {
  20.                 return 67;
  21.         }
  22.         public boolean equals(Object obj)
  23.         {
  24.                 if(!(obj instanceof Student))
  25.                 {
  26.                         throw new ClassCastException("类型不匹配");
  27.                 }
  28.                 Student s = (Student)obj;
  29.                 return this.name.equals(s.name) && this.age == s.age;
  30.         }
  31.         public String getName()
  32.         {
  33.                 return name;
  34.         }       
  35.         public int getAge()
  36.         {
  37.                 return age;
  38.         }
  39.         public String toString()
  40.         {
  41.                 return name+":"+age;
  42.         }
  43. }
  44. class MapTest
  45. {
  46.         public static void main(String[] args)
  47.         {
  48.                 HashMap<Student,String> hm = new HashMap<Student,String>();
  49.                 hm.put(new Student("lisi1",21),"beijing");
  50.                 hm.put(new Student("lhsi1",21),"beijing");
  51.                 hm.put(new Student("lisi2",22),"tianjin");
  52.                 hm.put(new Student("liti3",23),"nanjing");
  53.                 hm.put(new Student("lisi3",23),"nanjing");
  54.                 hm.put(new Student("lisi4",24),"dongjing");

  55.                 //第一种取出方式:
  56.                 Set<Student> keySet = hm.keySet();
  57.                 Iterator<Student> it = keySet.iterator();
  58.                 while(it.hasNext())
  59.                 {
  60.                         Student stu = it.next();
  61.                         String addr = hm.get(stu);
  62.                         System.out.println(stu+":"+addr);
  63.                 }
  64.                 //第二种取出方式:
  65.                 Set<Map.Entry<Student,String>> entrySet = hm.entrySet();
  66.                 Iterator<Map.Entry<Student,String>> iter = entrySet.iterator();
  67.                 while(iter.hasNext())
  68.                 {
  69.                         Map.Entry<Student,String> me = iter.next();
  70.                         Student stu = me.getKey();
  71.                         String addr = me.getValue();
  72.                         System.out.println(stu+":"+addr);
  73.                 }
  74.         }
  75. }
复制代码

评分

参与人数 1技术分 +1 收起 理由
黄玉昆 + 1

查看全部评分

回复 使用道具 举报
。。。你牛把视频代码都复制过来了。 可我比较的是value
回复 使用道具 举报
这位朋友,我觉得你犯了一个原则性错误!

   TreeMap(Comparator<? super K> comparator) ,即TreeMap的唯一性只有key,而value则不一定,该映射根据给定比较器进行排序。即只比较key!

     TreeMap<String,Integer> tm=new TreeMap<String,Integer>(); 对于你的问题,对这里面的数字排序 如果数字相同在按字符串排序  ?
     两种只能取其一即key部分,TreeMap<String,Integer> ,只按照String排序!TreeMap<Integer , String > 只按照Integer排序!
回复 使用道具 举报
我已经找到怎么排序value了
  1. TreeMap<Character,Integer> tm=new TreeMap<Character,Integer>();  
  2. ArrayList<Map.Entry<Character, Integer>> list = new ArrayList<Map.Entry<Character,Integer>>(tm.entrySet());
  3.                   Collections.sort(list,new Comparator<Map.Entry<Character, Integer>>(){

  4.                    public int compare(Entry<java.lang.Character, Integer> arg0,
  5.                      Entry<java.lang.Character, Integer> arg1) {
  6.                    return arg1.getValue()-arg0.getValue();
  7.                    }
  8.                    });
复制代码
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马