hashmap中(key,value),要求把value的值按从大到小排序,并且输出出来,该如何考虑?

解决方案 »

  1.   

    Map map = new HashMap();
    List list = new ArrayList(map.values());
    Collections.sort(list);
    如果value是object的话,就用
    Collections.sort(list, new Comparator() {
    public int compare(Object o1, Object o2) {
        return 0;// 自已写比较算法
    }});
      

  2.   

    http://community.csdn.net/Expert/topic/4428/4428752.xml?temp=.4011347Hashtable  v = new Hashtable();
    Object[] obj = v.values().toArray();
    // 排序
    Arrays.sort(obj, new Comparator() {public int compare(Object o1, Object o2) {
    Student stu1 = (Student)o1;
    Student stu2 = (Student)o2;return (int)(stu1.getScore() - stu2.getScore());
    }public boolean equals(Object obj) { return true ;}
    });
      

  3.   

    可是这样怎么能打印出名值对呢,感觉好像分离了,只是把value取出来了,那么原先的关系是不是被破坏了?
      

  4.   

    Map map = new HashMap();
    List list = new ArrayList(map.entrySet());
    Collections.sort(list, new Comparator() {
    public int compare(Object o1, Object o2) {
    Map.Entry obj1 = (Map.Entry) o1;
    Map.Entry obj2 = (Map.Entry) o2;
    obj1.getValue();
    obj2.getValue();
    }});

    for (Iterator iter = list.iterator(); iter.hasNext()) {
    Map.Entry entry = (Map.Entry) iter.next();
    entry.getKey();
    entry.getValue();
    }
      

  5.   

    Map map = new HashMap();
    List list = new ArrayList(map.entrySet());
    Collections.sort(list, new Comparator() {
    public int compare(Object o1, Object o2) {
    Map.Entry obj1 = (Map.Entry) o1;
    Map.Entry obj2 = (Map.Entry) o2;
    obj1.getValue();
    obj2.getValue();
    }});

    for (Iterator iter = list.iterator(); iter.hasNext()) {
    Map.Entry entry = (Map.Entry) iter.next();
    entry.getKey();
    entry.getValue();
    }
    -------------------------------------------------------------
    代码没写完吧
    Map map = new HashMap();
    List list = new ArrayList(map.entrySet());
    Collections.sort(list, new Comparator() {
    public int compare(Object o1, Object o2) {
    Map.Entry obj1 = (Map.Entry) o1;
    Map.Entry obj2 = (Map.Entry) o2;
    return obj1.getValue()<obj2.getValue();
    }});

    for (Iterator iter = list.iterator(); iter.hasNext()) {
    Map.Entry entry = (Map.Entry) iter.next();
    entry.getKey();
    entry.getValue();
    }
      

  6.   

    return obj1.getValue()<obj2.getValue();
    ----------------
    应该是return obj1.getValue().comparesTo(obj2.getValue());
      

  7.   

    return obj1.getValue()<obj2.getValue();和return obj1.getValue().comparesTo(obj2.getValue());都不能通过编译,还有,问一句Map.Entry obj1 = (Map.Entry) o1;Map.Entry是不是应该用实际使用实际使用的对象代替?