TreeSet排序

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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); }


结果:技术分享

方法二样例:

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之定制排序和自然排序

TreeSet ------自然排序与定制排序(比较器)

JavaSE基础 TreeSet集合(红黑树数据结构)

TreeSet类的排序问题

Java集合框架总结——TreeSet类的排序问题

Java基础——TreeSet