展开全部
// 如果使用Set进行排序,那么必须要满足以32313133353236313431303231363533e59b9ee7ad9431333264653433下两个条件:
// 1, 使用有序SET,即TreeSet
// 2, 被排序对象必须实现Comparable接口
// 这样做,其实是限制了排序的有效性(你可能不知道被排序对象是否实现了Comparable接口,而且你也不大可能要求程序中所有Model都实现这个接口)。
// 更好的排序方式是使用List进行排序,对List排序,只需要使用Collections.sort(list, comparator)静态方法。
// 第一个参数是要被排序的List,第二个参数是实现了Comparable接口的排序器。如果你要使用不同的排序方法,只需要替换第二个参数即可。
// 使用List进行排序,不需要被排序对象实现Comparable接口,而且排序算法是通过实现Comparable接口而来,这带来了极大的灵活性。
// 问题:我先将Set转为List,使用List排序后,再转为Set是否可行?
// 答案:基本不可行,如果将List转为无序Set,则不能保证顺序;若将List转为TreeSet,虽理论上说能保证顺序,但要求被排序对象实现Comparable接口。
// 可运行示例如下
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.TreeSet;
public class Question {
class Person{
private int age;
private String name;
public String toString(){
return name + ":" + age;
}
public int getAge(){
return age;
}
public Person(String name, int age){
this.name=name;
this.age = age;
}
}
class PersonSortable extends Person implements Comparable{
public PersonSortable(String name, int age){
super(name, age);
}
@Override
public int compareTo(Person o) {
int thisAge = this.getAge();
int otherAge = o.getAge();
return thisAge - otherAge;
}
}
class PersonComparator implements Comparator{
@Override
public int compare(Person o1, Person o2) {
int thisAge = o1.getAge();
int otherAge = o2.getAge();
return thisAge - otherAge;
}
}
public Question(){
System.out.println("无序SET, Person未实现Comparable, 示例。");
try{
Set personSet = new HashSet();
personSet.add(new Person("John", 15));
personSet.add(new Person("Smith", 25));
personSet.add(new Person("Lee", 35));
personSet.add(new Person("Peter", 45));
personSet.add(new Person("Dick", 55));
System.out.println(personSet);
}catch(Exception e){
System.out.println("发生异常:" + e.getMessage());
}
System.out.println("有序SET,Person未实现Comparable, 示例。");
try{
Set personSet = new TreeSet();
personSet.add(new Person("John", 15));
personSet.add(new Person("Smith", 25));
personSet.add(new Person("Lee", 35));
personSet.add(new Person("Peter", 45));
personSet.add(new Person("Dick", 55));
System.out.println(personSet);
}catch(Exception e){
System.out.println("发生异常:" + e.getMessage());
}
System.out.println("无序SET, PersonSortable实现了Comparable, 示例。");
try{
Set personSet = new HashSet();
personSet.add(new PersonSortable("John", 15));
personSet.add(new PersonSortable("Smith", 25));
personSet.add(new PersonSortable("Lee", 35));
personSet.add(new PersonSortable("Peter", 45));
personSet.add(new PersonSortable("Dick", 55));
System.out.println(personSet);
}catch(Exception e){
System.out.println("发生异常:" + e.getMessage());
}
System.out.println("有序SET,PersonSortable实现了Comparable, 示例。");
try{
Set personSet = new TreeSet();
personSet.add(new PersonSortable("John", 15));
personSet.add(new PersonSortable("Smith", 25));
personSet.add(new PersonSortable("Lee", 35));
personSet.add(new PersonSortable("Peter", 45));
personSet.add(new PersonSortable("Dick", 55));
System.out.println(personSet);
}catch(Exception e){
System.out.println("发生异常:" + e.getMessage());
}
System.out.println("使用Collection.sort进行排序示例。");
try{
List personList = new ArrayList();
personList.add(new Person("John", 15));
personList.add(new PersonSortable("Smith", 25));
personList.add(new Person("Lee", 35));
personList.add(new PersonSortable("Peter", 45));
personList.add(new Person("Dick", 55));
Collections.sort(personList, new PersonComparator());
System.out.println(personList);
System.out.println("\t将排序后的结果封装回无序SET:");
Set personSetUnSortable = new HashSet();
for(Person person : personList){
personSetUnSortable.add(person);
}
System.out.println("\t" + personSetUnSortable);
System.out.println("\t将排序后的结果封装回有序SET:");
Set personSetSortable = new TreeSet();
for(Person person : personList){
personSetSortable.add(person);
}
System.out.println("\t" + personSetSortable);
}catch(Exception e){
System.out.println("发生异常:" + e.getMessage());
}
}
public static void main(String args[]){
new Question();
}
}
本回答由提问者推荐
已赞过
已踩过<
你对这个回答的评价是?
评论
收起