码迷,mamicode.com
首页 > 编程语言 > 详细

TreeSet排序

时间:2014-11-20 17:06:20      阅读:171      评论:0      收藏:0      [点我收藏+]

标签:treeset排序

TreeSet的排序可以通过两种方法来实现:

1.通过TreeSet(Comparator<? super E> comparator) 构造方法指定TreeSet的比较器进行排序;

2.使用TreeSet()构造方法,并对需要添加到set集合中的元素实现Comparable接口进行排序


方法一例子:

person类:

public class Person {
	private String name;
	private int age;
	private String date;
	
	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 String getDate() {
		return date;
	}
	public void setDate(String date) {
		this.date = date;
	}
	public Person(String name) {
		this.name=name;
	}
	public Person() {
	}
	
	@Override
	public String toString() {
		return "Person [name=" + name + ", age=" + age + ", date=" + date + "]";
	}
	public Person(String name, int age, String date) {
		super();
		this.name = name;
		this.age = age;
		this.date = date;
	}
	
}

自己实现比较器:

import java.util.Comparator;
public class MyComparator implements Comparator<Person>{
	@Override
	public int compare(Person p1, Person p2) {
		if(p1.getAge()>p2.getAge()){
			return 1;
		}else if(p1.getAge()==p2.getAge()){
			return 0;
		}else{
			return -1;
		}
	}

}

测试数据:

@Test
	public void treeSet(){//所有元素总是根据指定排序规则保持有序状态。
		Set<Person> set = new TreeSet<Person>(new MyComparator());
		set.add(new Person("aaa", 67, "254"));
		set.add(new Person("bbb", 16, "254"));
		set.add(new Person("ccc", 1, "254"));
		set.add(new Person("ddd", 675, "254"));
		System.out.println(set); 
	}

结果:bubuko.com,布布扣

方法二例子:

person类,需要实现Comparable接口,并重写compareTo()方法,compareTo方法中定义排序的方式

public class Person2 implements Comparable {
	private String name;
	private int age;
	private String date;

	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 String getDate() {
		return date;
	}

	public void setDate(String date) {
		this.date = date;
	}

	public Person2(String name) {
		this.name = name;
	}

	public Person2() {
	}


	@Override
	public String toString() {
		return "Person [name=" + name + ", age=" + age + ", date=" + date + "]";
	}

	public Person2(String name, int age, String date) {
		super();
		this.name = name;
		this.age = age;
		this.date = date;
	}

	@Override
	public int compareTo(Object obj) {
		if (obj instanceof Person2) {//instanceof判断内存中实际对象obj是不是Person2类型
			Person2 p = (Person2) obj;
			if (this.age > p.getAge()) {
				return 1;
			} else if (this.age == p.getAge()) {
				return 0;
			} else {
				return -1;
			}
		}
		return 0;
	}

}
测试数据:
@Test
	public void treeSet2(){
		Set<Person2> set = new TreeSet<Person2>();
		set.add(new Person2("aaa", 67, "254"));
		set.add(new Person2("bbb", 16, "254"));
		set.add(new Person2("ccc", 1, "254"));
		set.add(new Person2("ddd", 675, "254"));
		System.out.println(set); 
	}

测试结果:

[Person [name=ccc, age=10, date=254], Person [name=eee, age=12, date=254], Person [name=bbb, age=16, date=254], Person [name=aaa, age=67, date=254], Person [name=ddd, age=675, date=254]]


TreeSet排序

标签:treeset排序

原文地址:http://blog.csdn.net/xiaoyi_tdcq/article/details/41315177

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!