个性集合之TreeSet类的元素有序性排序和元素唯一性的特点

 

      TreeSet类是一个具体的类,实现了Set接口,同时Set接口实现Collection接口,三者均带有泛型标志,TreeSet类能够对元素进行有序排序和保证元素不重复存入集合,主要有两条路可以走。

      第一,自然排序法,仅仅使用TreeSet的无参构造方法,同时在元素类中实现Comparable<E>接口,同时,复写该接口的CompareTo(T t)方法。那么,具体的排序规则,我们将可以在该复写方法中自行定义,注意,该方法int   compareTo(T t)带有返回值类型。方法的说明如下:比较此对象与指定对象的顺序。如果该对象小于、等或大于指定对象,则分别返回负整数、零或正整数。

     第二条路,使用带参数构造器,可以说这是集合自带比较器的方法,即TreeSet<E> ts=new TreeSet<E>(Comparator comparator);其中参数(Comparator comparator)是接口类型的参数,形式参数是一个接口类型或者类类型,说明,这里需要的是一个具体实现类对象,所以,这里可以采取匿名对象的形式,并复写接口Comparator的compare(T t1,T t2)方法。那么在该方法体中我们就可以详细写出排序规则。使用匿名对象用完既是垃圾,不占内存。

      两种方法比较,第一种方法比较冗余,不方便,第二种,使用匿名对象,同时复写方法,任何排序规则改动只需在这个地方改即可,比较方便。第一种方法(自然排序法),排序规则的改动需要到元素类中去改动,该排序规则相对于第二种走的路,第一种走的路较多。所以,推荐使用第二种方法。第一种方法,元素类实现相应接口,复写相应方法,方法内定义规则,说白了,就是元素本身具备比较器。第二种方法,说白了,就是集合本身自带比较器。

     TreeSet类的数据结构是红黑二叉树,所以,它能够保证元素的排序和保证元素的不重复,即唯一性。下面给出两种方法的相应代码比较。

        第一种方法,采用自然排序:

/********************************************************************/
import java.util.TreeSet;

//使用TreeSet集合 的自然排序法对元素进行排序
public class TreeSetDemo1 {
	public static void main(String[] args) {
		TreeSet<Student> ts = new TreeSet<Student>();
		ts.add(new Student("曹操", 34));
		ts.add(new Student("刘备", 36));
		ts.add(new Student("关羽", 34));
		ts.add(new Student("张飞", 33));
		ts.add(new Student("司马懿", 37));
		ts.add(new Student("董卓", 39));
		ts.add(new Student("赵子龙", 32));
		for (Student i : ts) {
			System.out.println(i.getName() + " " + i.getAge());
		}
	}
}
/********************************************************************/
/**************************************************************/
public class Student implements Comparable<Student> {
	@Override
	public int compareTo(Student s) {
		int num1 = this.getName().length() - s.getName().length();
		int num2 = num1 == 0 ? this.getName().compareTo(s.getName()) : num1;
		int num3 = num2 == 0 ? this.getAge() - s.getAge() : num2;
		return num3;
	}

	private String name;
	private int age;

	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 Student(String name, int age) {
		super();
		this.name = name;
		this.age = age;
	}

}
/**************************************************************/

     第二中,比较器:

import java.util.Comparator;
import java.util.TreeSet;

//使用比较器
public class TreeSetDemo2 {
	public static void main(String[] args) {
		TreeSet<Student> ts = new TreeSet<Student>(new Comparator<Student>() {
			@Override
			public int compare(Student s1, Student s2) {
				int num1 = s1.getName().length() - s2.getName().length();
				int num2 = num1 == 0 ? s1.getName().compareTo(s2.getName()): num1;
				int num3 = num2 == 0 ? s1.getAge() - s2.getAge() : num2;
				return num3;
			}
			});
		ts.add(new Student("曹操", 34));
		ts.add(new Student("刘备", 36));
		ts.add(new Student("关羽", 34));
		ts.add(new Student("张飞", 33));
		ts.add(new Student("司马懿", 37));
		ts.add(new Student("董卓", 39));
		ts.add(new Student("赵子龙", 32));
		for (Student i : ts) {
			System.out.println(i.getName() + " " + i.getAge());
		}
	}
}
public class Student {
	
	private String name;
	private int age;

	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 Student(String name, int age) {
		super();
		this.name = name;
		this.age = age;
	}

}

 

转载于:https://my.oschina.net/u/3384770/blog/866558

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值