java set int 排序_set排序比较的是什么?java

展开全部

// 如果使用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();

}

}

本回答由提问者推荐

2Q==

已赞过

已踩过<

你对这个回答的评价是?

评论

收起

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值