hibernate的set集合 怎么有序输出???

specturm 2010-11-25 03:47:38
<set name="waySet" sort="unsorted" inverse="false" mutable="true"
optimistic-lock="true" embed-xml="true" lazy="false" fetch="join">
<key column="bureauID" on-delete="noaction" />
<one-to-many class="xx.CodeLampWay"
not-found="exception" embed-xml="true" />

搜了下网上的设置 ,都出错··
求教··
...全文
451 15 打赏 收藏 转发到动态 举报
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
specturm 2010-11-26
  • 打赏
  • 举报
回复
集合元素是对象的话 重写compareTo方法 实现了排序
谢谢解答
宁波朱超 2010-11-26
  • 打赏
  • 举报
回复
以上是按person类中的age来排序的,也可以按name
具体的情况你可以按你具体的字段来操作.
宁波朱超 2010-11-26
  • 打赏
  • 举报
回复
Person类 和 Test类

public class Person implements Comparable<Person> {//实现这个接口!!!!!!


public String name;
public String getName() {
return name;
}


public void setName(String name) {
this.name = name;
}


public int getAge() {
return age;
}


public void setAge(int age) {
this.age = age;
}
public int age;

public Person(){

}

public Person(String name,int age){
this.name=name;
this.age=age;
}

}
@Override
public int compareTo(Person o) {
return this.age-o.age;
}
}



import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class Test {
public static void main(String[] args) {
Person person = new Person("person1",1);
Person person2 =new Person("person2",2);
Person person3 =new Person("person3",3);
Person person4 =new Person("person4",4);
Person person5 =new Person("person5",5);
Person person6 =new Person("person8",8);
Person person7 =new Person("person7",7);

List<Person>list=new ArrayList<Person>();
list.add(person);
list.add(person7);
list.add(person2);
list.add(person3);
list.add(person4);
list.add(person5);
list.add(person6);

Collections.sort(list);
for (Person xPerson : list) {
System.out.println(xPerson.getName()+" "+xPerson.getAge());
}
System.out.println("==============================");
Collections.reverse(list);
for (Person xPerson : list) {
System.out.println(xPerson.getName()+" "+xPerson.getAge());
}
}
}




结果:
person1 1
person2 2
person3 3
person4 4
person5 5
person7 7
person8 8
==============================
person8 8
person7 7
person5 5
person4 4
person3 3
person2 2
person1 1
宁波朱超 2010-11-26
  • 打赏
  • 举报
回复
[Quote=引用 10 楼 specturm 的回复:]
引用 8 楼 zhuchao_ko 的回复:
引用 7 楼 cgliuyu 的回复:
set 有序么?



Java code

List<String>list=new ArrayList<String>();
list.add("f");
list.add("b");
list.add("c");
list.add("e");

……


我的是对象 Set……
[/Quote]

不好意思 上班比较忙 再多点的代码来不及了

http://lavasoft.blog.51cto.com/62575/68380

这个是关于对象排序的 你一定有用。
melc521 2010-11-26
  • 打赏
  • 举报
回复
set
<set name=“addresses” lazy=“true” table=“t_address”
sort=“natural”>
<key column=“user_id” />
<element type=“string” column=“country” />
</set>
sort=“natural”指定采用Java默认排序机制,它会调用相应数据类型的compareTo方法进行排序中的值比对。此处<element type=“string” coumn=“country”>,指定了元素类型为string,则将基于String.compareTo方法对country进行排序。
通过实现java.util.Comparator接口,可以实现自定义排序算法。
specturm 2010-11-26
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 zhuchao_ko 的回复:]
引用 7 楼 cgliuyu 的回复:
set 有序么?



Java code

List<String>list=new ArrayList<String>();
list.add("f");
list.add("b");
list.add("c");
list.add("e");

……
[/Quote]

我的是对象 Set<way> way是对象 该怎么弄··
mouhk 2010-11-26
  • 打赏
  • 举报
回复
使用list,bag等有序集合吧。
宁波朱超 2010-11-26
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 cgliuyu 的回复:]
set 有序么?
[/Quote]


List<String>list=new ArrayList<String>();
list.add("f");
list.add("b");
list.add("c");
list.add("e");

for (String string : list) {
System.out.print(string+" ");
}

System.out.println();

Set<String>set=new TreeSet<String>(list);
for (String string : set) {
System.out.print(string+" ");
}



结果:
f b c e
b c e f
cgliuyu 2010-11-25
  • 打赏
  • 举报
回复
set 有序么?
s0s0s0s 2010-11-25
  • 打赏
  • 举报
回复
用list
tosshl 2010-11-25
  • 打赏
  • 举报
回复

可以用LinkedHashSet保持插入时的顺寻
或者用楼上说的TreeSet
宁波朱超 2010-11-25
  • 打赏
  • 举报
回复
SET...TREESET 楼主用过没。。。哈哈
specturm 2010-11-25
  • 打赏
  • 举报
回复
order-by 具体怎么设 网上的都显示不了··
wanwang8 2010-11-25
  • 打赏
  • 举报
回复
set 本身是无序的。hibernate没做扩展把,想要让有序排列可以 设置下order-by
tanzhoujavaeelover 2010-11-25
  • 打赏
  • 举报
回复
什么错,能具体一点吗?

81,092

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧