如贴子所说:
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));求解决方法!
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));求解决方法!
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
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));
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是没办法解决的
那他new Integer(4)应该不会丢的啊?怎么回事呢?
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]);
}
}
?
楼上的大虾 楼主帖里给出的是map.put(new Integer(4), new Content("3","wangwu",3));啊
那他new Integer(4)应该不会丢的啊?怎么回事呢?这样当然没丢!!!!你没看我给的例子吗?????就这么个破问题磨叽了这么半天!!!!
实在不行,楼主你就自己实现一个map类,爱怎么实现怎么实现。
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);
}
程序如下: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排序,麻烦各位再指点一下@
id: 7, name: 儿子节点, order: 1
id: 15, name: 孙子节点, order: 11
不是按照后面的Object排序的!!!!!!!你要保证你的key和你的order是一致的不就行了!!!!!!笨死算了!!!!!!!
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));你再看看结果!!!!!!!
另外:
imA兄的话:你要保证你的key和你的order是一致的不就行了!!!!!!这一句严重不对啊!key和order怎么能相提并论啊
map.put(new Integer(11), new Content("3","7","14","孙子","孙子",11));
这是11 这也是11
明白了???......我也要崩溃了~
TreeMap排序是按照key排序的 而你想让他按order排序输出
那你把key设成和order一样 不就相当于按order排序输出么???!!!
key 是 1 order 也是 1
key 是 2 order 也是 2
TreeMap排序是按照key排序 那输出的时候第一个是不是就是在第2个前面了???是不是就相当于按order排序了???
崩溃中请勿打扰~。
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文档中都有详细的说明。你这样问,很多人是没有耐心给你从基础讲起的。祝顺利!!!!!拜拜!!!!!!