如贴子所说:
http://community.csdn.net/Expert/topic/5476/5476728.xml?temp=.1121942imA 兄已经给出了例子,如下:
 public class TestSortMap {
public static void main(String[] args){
java.util.Map map=new java.util.TreeMap();
// java.util.Map map=new java.util.HashMap();
map.put(new Integer(2),new Content("1","zhangsan",2));
map.put(new Integer(1), new Content("2","lisi",1));
map.put(new Integer(3), new Content("3","wangwu",3));
java.util.Iterator i=map.keySet().iterator();
while(i.hasNext())
System.out.println(map.get(i.next()).toString());
}
}class Content{
String id; String name; int order; public Content(String id, String name, int order) {
this.id = id;
this.name = name;
this.order = order;
}
public String toString()
{
return "id: "+id+", name: "+name+", order: "+order;
}
}
 
但有个问题,如果order相等的话,就少对像了,怎么解决啊?比如再多一个
map.put(new Integer(4), new Content("3","wangwu",3));求解决方法!

解决方案 »

  1.   

    package com.forecast.feng.test;public class TestSortMap {
    public static void main(String[] args){
    java.util.Map map=new java.util.TreeMap();
    // java.util.Map map=new java.util.HashMap();
    map.put(new Integer(2),new Content("1","张三",2));
    map.put(new Integer(1), new Content("2","李四",1));
    map.put(new Integer(3), new Content("3","王五",3));
    map.put(new Integer(4), new Content("3","赵六",3));
    java.util.Iterator i=map.keySet().iterator();
    while(i.hasNext())
    System.out.println(map.get(i.next()).toString());
    }
    }class Content{
    String id; String name; int order; public Content(String id, String name, int order) {
    this.id = id;
    this.name = name;
    this.order = order;
    }
    public String toString()
    {
    return "id: "+id+", name: "+name+", order: "+order;
    }
    }
    输出结果:
    id: 2, name: 李四, order: 1
    id: 1, name: 张三, order: 2
    id: 3, name: 王五, order: 3
    id: 3, name: 赵六, order: 3
      

  2.   

    原帖中
    HashMap hm= new HashMap();
    hm.put("1",new content("1","张三",2));
    hm.put("2",new content("2","张四",1));
    hm.put("3",new content("3","张五",3));改成
    LinkedHashMap hm= new LinkedHashMap();//LinkedHashMap有序 输出时会按输入的顺序显示
    hm.put("1",new content("1","张三",2));
    hm.put("2",new content("2","张四",1));
    hm.put("3",new content("3","张五",3));
      

  3.   

    哦 按order输出 会少???
      

  4.   

    public class TestSortMap {
    public static void main(String[] args){
    java.util.Map map=new java.util.TreeMap();
    // java.util.Map map=new java.util.HashMap();
    map.put(new Integer(2),new Content("1","张三",2));
    map.put(new Integer(1), new Content("2","李四",1));
    map.put(new Integer(3), new Content("3","王五",3));
    map.put(new Integer(3), new Content("3","赵六",3));//注意,这里和我上面给你的程序不同
    java.util.Iterator i=map.keySet().iterator();
    while(i.hasNext())
    System.out.println(map.get(i.next()).toString());
    }
    }class Content{
    String id; String name; int order; public Content(String id, String name, int order) {
    this.id = id;
    this.name = name;
    this.order = order;
    }
    public String toString()
    {
    return "id: "+id+", name: "+name+", order: "+order;
    }
    }楼主说的是这样吧,这样的输出结果是:
    id: 2, name: 李四, order: 1
    id: 1, name: 张三, order: 2
    id: 3, name: 赵六, order: 3就把王五那条记录丢了。
    这是因为map是根据键值来存储对象的,因为王五和赵六使用的是同样的键值(都是new Integer(3))来存储在map中的,所以后来的就覆盖了前面的。这个问题map是没办法解决的
      

  5.   

    楼上的大虾 楼主帖里给出的是map.put(new Integer(4), new Content("3","wangwu",3));啊
    那他new Integer(4)应该不会丢的啊?怎么回事呢?
      

  6.   

    这样:
    class Content implements Comparable {
    Integer id; String name; int order; public Content(Integer id, String name, int order) {
    this.id = id;
    this.name = name;
    this.order = order;
    }
    public String toString()
    {
    return "id: "+id+", name: "+name+", order: "+order;
    }
             public int compareTo(Object obj) {
                      Content other = (Content) obj;
                      if (id < other.id) return -1;
                      if (id > other.id) return 1;
                      return 0; 
             }
    }public class TestSortMap {
    public static void main(String[] args){
    Content[] contents = new Content[10];
    contents[0] = new Content("1","zhangsan",2);
    contents[1] = new Content("2","lisi",1);
    contents[2] = new Content("3","wangwu",3);
             Arrays.sort(contents);
    for (int i = 0; i < contents.length; i++) {
        System.out.println(contents[i]);
             }
    }
      

  7.   

    TreeMap也是有序输出的,楼主可以参考一下.
      

  8.   

    回复人:WIN_ANGEL(WIN_ANGEL) ( 二级(初级)) 信誉:100  2007-04-19 13:00:05  得分:0

    楼上的大虾 楼主帖里给出的是map.put(new Integer(4), new Content("3","wangwu",3));啊
    那他new Integer(4)应该不会丢的啊?怎么回事呢?这样当然没丢!!!!你没看我给的例子吗?????就这么个破问题磨叽了这么半天!!!!
    实在不行,楼主你就自己实现一个map类,爱怎么实现怎么实现。
      

  9.   

    HOHO 楼上的大虾不要生气啊 我只是纳闷既然那样不会丢 但楼主却说会丢 所以我不明白 不知道楼主是怎么运行的
      

  10.   

    do like this:
      map.put(key,object) o -> ArrayList
      if(map.contains(tempkey)) {
            ArrayList tempList = (ArrayList)map.get(tempkey)
            tempList.add(new Object());
            map.put(key,tempList);
      }else{
            map.put(key,object);
      }
      

  11.   

    实在是不好意思,还是有些问题:
    程序如下:package com.dtree;public class TestSortMap {
    public static void main(String[] args){
    java.util.Map map=new java.util.TreeMap();
    // java.util.Map map=new java.util.HashMap();
    map.put(new Integer(3), new Content("2","3","2","儿子","儿子",2));
    map.put(new Integer(7), new Content("1","7","2","儿子","儿子",1));
    map.put(new Integer(15), new Content("3","7","14","孙子","孙子",11));
    java.util.Iterator i=map.keySet().iterator();
    while(i.hasNext())
    System.out.println(map.get(i.next()).toString());
    }
    }class Content{
        private String id ;             
        private String pid ;            
        private String name ;           
        private String content ;     
        private String title ;         
        private int order;
    public Content(String id,String pid, String name,String content,String title,int hit) {
    this.id = id;
    this.pid = pid;
    this.name = name;
    this.content = content;
    this.title = title;
    this.order = hit;
    }
    public String toString()
    {
    return "id: "+id+", name: "+name+", order: "+order;
    }
    }实际上没有按order排序,麻烦各位再指点一下@
      

  12.   

    输出为:id: 3, name: 儿子节点, order: 2
    id: 7, name: 儿子节点, order: 1
    id: 15, name: 孙子节点, order: 11
      

  13.   

    你搞错了TreeMap的意义,它是按照Key来排序而不是Value,所以答案是对的而且你的类也没有实现Comparable接口,JVM不可能知道你需要按什么排序,你必须手工实现该方法
      

  14.   

    可是imA兄给的结果确实是对的啊,难到是凑巧???
      

  15.   

    晕了,TreeMap排序是按照key排序的!!!!!!!
    不是按照后面的Object排序的!!!!!!!你要保证你的key和你的order是一致的不就行了!!!!!!笨死算了!!!!!!!
      

  16.   

    map.put(new Integer(3), new Content("2","3","2","儿子","儿子",2));
    map.put(new Integer(7), new Content("1","7","2","儿子","儿子",1));
    map.put(new Integer(15), new Content("3","7","14","孙子","孙子",11));
    改为:map.put(new Integer(2), new Content("2","3","2","儿子","儿子",2));
    map.put(new Integer(1), new Content("1","7","2","儿子","儿子",1));
    map.put(new Integer(11), new Content("3","7","14","孙子","孙子",11));你再看看结果!!!!!!!
      

  17.   

    HOHO~ 楼上的大虾真怒了 楼主你再好好看看程序 楼上大虾说的很对 你把key和order整成一样的不就完了么
      

  18.   

    HOHO hero222(笑傲江湖) 我前面已经说过了 我刚看的时候也以为是 但这个问题好像跟LinkedHashMap没关系 imA(男的不会,会的不男) 大虾说的对
      

  19.   

    不对啊,我order可以重复,key就不可以啊
      

  20.   

    我再试前台表格是不是可以实现自动排序,可以的话,就不用后台来实现了!我会尽快结贴!
    另外:
    imA兄的话:你要保证你的key和你的order是一致的不就行了!!!!!!这一句严重不对啊!key和order怎么能相提并论啊
      

  21.   

    服了 谁说让你key重复了?是说让你把key和order设成一样的
    map.put(new Integer(11), new Content("3","7","14","孙子","孙子",11));
                      这是11                                       这也是11
    明白了???......我也要崩溃了~
      

  22.   

    额地神啊~楼主大哥啊 ~
    TreeMap排序是按照key排序的 而你想让他按order排序输出 
    那你把key设成和order一样 不就相当于按order排序输出么???!!!
    key 是 1 order 也是 1
    key 是 2 order 也是 2
    TreeMap排序是按照key排序 那输出的时候第一个是不是就是在第2个前面了???是不是就相当于按order排序了???
    崩溃中请勿打扰~。
      

  23.   

    to楼主:public class TestSortMap {
    public static void main(String[] args) {
    java.util.List list=new java.util.ArrayList();
    list.add(new Content("1","张三",4));
    list.add(new Content("2","张三",3));
    list.add(new Content("3","张三",2));
    list.add(new Content("4","张三",5));
    Object[] o=list.toArray();
    java.util.Arrays.sort(o);
    for(int i=0;i<o.length;i++)
    System.out.println(o[i]);
    }
    }class Content implements Comparable {
    String id; String name; int order; public Content(String id, String name, int order) {
    this.id = id;
    this.name = name;
    this.order = order;
    } public String toString() {
    return "id: " + id + ", name: " + name + ", order: " + order;
    } public int compareTo(Object o) {
    int theOrder = ((Content) o).order;
    if (order < theOrder)
    return -1;
    else if (order == theOrder)
    return 0;
    else
    return 1;
    }
    }
    应该可以解决你的问题!
    另外,遇到问题首先要查看java的API文档,别什么都问,你上面的不明白的地方在API文档中都有详细的说明。你这样问,很多人是没有耐心给你从基础讲起的。祝顺利!!!!!拜拜!!!!!!
      

  24.   

    楼上的大虾果然身经百战 百毒不侵 心理素质比我这样刚来CSDN的菜鸟好多了......我拜你为师吧 HOHO~ 说真地~