我在hashmap里保存如下内容:
class content{
String id ;
String name;
int order;
public content(){
this.id =id
this.name=name;
this.order=order;
}
}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));我想问一下,现在如何我想换content对像中的order排序输出,有没有好办法啊?
谢谢各位啊!
class content{
String id ;
String name;
int order;
public content(){
this.id =id
this.name=name;
this.order=order;
}
}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));我想问一下,现在如何我想换content对像中的order排序输出,有没有好办法啊?
谢谢各位啊!
你可以用sortmap
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;
}
}