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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 张小明   /  2011-7-16 03:29  /  6412 人查看  /  7 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

请教TreeMap<k,V>如何根据值来排序?TreeMap<K,V>默认是用键key按自然顺序来排序的,现在有个类TreeMap<Character,Interger>,这个类按键的字母顺序给元素排序,那怎样按其Interger类型的value值排序呢?查阅JDk文档发现TreeMap有构造方法能根据给定的比较器排序,怎么实现呢?表述可能不太清楚,还请谅解。

评分

参与人数 1技术分 +1 收起 理由
admin + 1 好问题!!

查看全部评分

7 个回复

倒序浏览
黑马网友  发表于 2011-7-16 14:42:36
沙发
个人感觉map里涉及到的排序都是与set或list有关,一般通过把key放set集合,对set集合进行排序,来实现map的排序,因为遍历输出时是按已经排好序的set集合里的key来输出的,本身map里的元素是不会变动顺序的。
    要根据value排序。估计要先把value放某一集合或数组,只要能实现排序功能的容器都行,比如放set集合里,排好序后按value的顺序,遍历时在map里找到对应的key,从排好序的第一value开始,第一个value找到并输出对应的key,第二个value找到相对应的key。不过value可以对多个key,输出key时估计有点麻烦,由value找key也不好实现。

评分

参与人数 1技术分 +1 收起 理由
admin + 1 虽然你答得不对,也送1分,再接再励

查看全部评分

回复 使用道具 举报
实现集合的排序可是实现Comparator<T>或者 Comparable<T>
我用Comparator<T>试验了一下,代码如下:
import java.util.*;
public class TreeMapSorted implements Comparator<Map.Entry<String,Integer>>
{

    public static void main(String[] args)
    {
        // TODO code application logic here
        TreeMap<String,Integer> treeMap=new TreeMap<String,Integer>();
        treeMap.put("zhangsan",4);
        treeMap.put("lisi", 3);
        treeMap.put("wangwu",1);
        treeMap.put("zhaoliu", 5);
        treeMap.put("taylor", 2);
        
        //未排序前
        Set<Map.Entry<String,Integer>> set=treeMap.entrySet();
        for(Map.Entry<String,Integer> entry:set)
        {
        System.out.println("key=" + entry.getKey() + ", value=" + entry.getValue());
        }
        
        //排序后
        TestTreeMapSort(treeMap);

    }
@Override
public int compare(Map.Entry<String, Integer> o1, Map.Entry<String, Integer> o2)
  {
    return o1.getValue().intValue() - o2.getValue().intValue();
  }

public static void TestTreeMapSort(TreeMap<String,Integer> map)
{
      List<Map.Entry<String,Integer>> list = new ArrayList<Map.Entry<String,Integer>>(map.entrySet());

      //public static <T> void sort(List<T> list,Comparator<? super T> c)
      //文档表明Comparator的类型参数至少等于集合的对象或者是它的超类
      Collections.sort(list, new TreeMapSorted());

//    Iterator<Map.Entry<String,Integer>> i = list.iterator();
//      while (i.hasNext()) {
//      Map.Entry<String, Integer> entry = i.next();
//     System.out.println("key=" + entry.getKey() + ", value=" + entry.getValue());
//     }

    for(Map.Entry<String,Integer> entry:list)
    {
        System.out.println("key=" + entry.getKey() + ", value=" + entry.getValue());
    }

}
}不会插图,插不进运行结果
[ 本帖最后由 BlackHorse 于 2011-07-16  20:09 编辑 ]

评分

参与人数 1技术分 +3 收起 理由
admin + 3 答得好,必须给3分

查看全部评分

回复 使用道具 举报
黑马网友  发表于 2011-7-17 01:53:05
板凳
两种方法,
一个是集合中元素的类实现 Comparable<T>接口
还有一个方法是,建一个比较器,实现Comparator接口,

评分

参与人数 1技术分 +1 收起 理由
admin + 1 没说错!!

查看全部评分

回复 使用道具 举报
黑马网友  发表于 2011-7-17 14:28:35
报纸
冒泡排序行不?给你个例子,自己琢磨
public static void main(String[] args) {
                List<Student> list = new ArrayList<Student>();

                Student student1 = new Student("张三1", 21, 79);
                Student student2 = new Student("张三2", 15, 100);
                Student student3 = new Student("张三3", 23, 66);
                Student student4 = new Student("张三4", 12, 71);
                Student student5 = new Student("张三5", 17, 99);

                list.add(student1);
                list.add(student2);
                list.add(student3);
                list.add(student4);
                list.add(student5);
                Student temp ;
                for (int i = 0; i < list.size(); i++) {
                        for (int j = 0; j + 1 < list.size() - i; j++) {
                                if (list.get(j).getScore() < list.get(j+1).getScore()) {
                                        temp = (Student) list.get(j);
                                        list.set(j, list.get(j+1));
                                        list.set((j+1), (Student) temp);
                                }
                        }
                }
                System.out.println("成绩由高到低排序:");
                for(Student stu : list){
                        System.out.println(stu.getName()+" 的成绩是: " +stu.getScore());
                }
        }

评分

参与人数 1技术分 +1 收起 理由
admin + 1

查看全部评分

回复 使用道具 举报
我也查阅了一下API的确像楼主所说的那样
我先拷贝一下吧。
java.util
类 TreeMap<K,V>
基于红黑树(Red-Black tree)的 NavigableMap 实现。该映射根据其键的自然顺序进行排序,或者根据创建映射时提供的 Comparator 进行排序,具体取决于使用的构造方法。

然后我查看了一下他的构造方法。有4个构造方法。
TreeMap()
          使用键的自然顺序构造一个新的、空的树映射。
TreeMap(Comparator<? super K> comparator)
          构造一个新的、空的树映射,该映射根据给定比较器进行排序。
TreeMap(Map<? extends K,? extends V> m)
          构造一个与给定映射具有相同映射关系的新的树映射,该映射根据其键的自然顺序 进行排序。
TreeMap(SortedMap<K,? extends V> m)
          构造一个与指定有序映射具有相同映射关系和相同排序顺序的新的树映射。

    其中第二个构造方法要传入一个comparator比较器。我想就是根据创建映射时提供的 Comparator 进行排序。也就是前面所写的根据创建映射时提供的 Comparator 进行排序
     我想,他这个肯定是由构造方法决定如何对TreeMap里面的值进行排序,如果按照第二种传入comparator对象,那就会按照comparator排序,如果按照第三种,就会根据它的key进行自然排序,一切取决于他的构造方法

评分

参与人数 1技术分 +1 收起 理由
admin + 1 迟来的奖励

查看全部评分

回复 使用道具 举报
黑马网友  发表于 2011-7-22 14:58:04
7#
技术测试
6、使用第5题定义的学生类创建5个对象, 属性可为任意值. 编程对这5个对象按成绩排序, 并将结果输出。


自己有答案,怕在这公布不妥
回复 使用道具 举报
黑马网友  发表于 2011-7-22 16:03:22
8#
我有个Map排序的贴子,杨志罡那个代码写的不错。当然,里边有我写的一个思路…张伟说的那个比较器传进去的指名了比较的是K,不能实现。我是常帅,那天给你发的有关MAP排序的网页还保留的话就发一下。我手机上网,很无力

评分

参与人数 1技术分 +1 收起 理由
小龙 + 1 用手机的回帖的人还挺多的!

查看全部评分

回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马