/*
TreeSet集合是Set集合的一个子实现类,它是基于TreeMap中的NavigableSet接口实现的
TreeSet集合是默认通过自然排序将集合中的元素进行排序
TreeSet有两种排序方式:
1)自然排序
2)比较器排序
*/
public class Student implements Comparable<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;
}
@Override
public String toString() {
return "Student [name=" + name + ", age=" + age + "]";
}
public Student() {
super();
}
public Student(String name, int age) {
super();
this.name = name;
this.age = age;
} /*
* 在compareTo()方法中要实现Student类型的排序规则,指定排序规则 首先按照age升序排列 age相同,按照name升序排序
*/
@Override
public int compareTo(Student o) {
//如果该对象小于等于或者大于指定对象,则分别返回负整数,零或者正整数
if (this.age < o.age) {
return -1;
}else if (this.age > o.age) {
return 1;
}
if (this.name.compareTo(o.name) > 0) {
return 1;
}else if (this.name.compareTo(o.name) < 0) {
return -1;
}
return 0;
}
}
import java.util.TreeSet;
//TreeSet可以保证添加到其中的String类型的数据不可重复且可以排序
public class TreeSetTest {
/*
* 我们要把Student类型的数据添加到TreeSet中,则Student要实现comparable接口并且要实现其中的compareTo()方法
* 在compareTo()方法中要实现Student类型的排序规则
* 当我们把Student类型的数据向TreeSet中添加的时候,TreeSet会自动的调用Student对象的compareTo()方法
* 如果compareTo()方法返回0,则不让数据进入集合;然后根据compareTo()方法返回负数/正数对象数据进行排序
*/
public static void test(){
Set<Student> set = new TreeSet<Student>();
Student stu1 = new Student("aa",21);
Student stu2 = new Student("bb",21);
Student stu3 = new Student("aa",22);
Student stu4 = new Student("aa",23);
Student stu5 = new Student("dd",24);
Student stu6 = new Student("ee",25);
Student stu7 = new Student("ee",26);
Student stu8 = new Student("ff",24);
Student stu9 = new Student("aa",21);
set.add(stu1);
set.add(stu2);
set.add(stu3);
set.add(stu4);
set.add(stu5);
set.add(stu6);
set.add(stu7);
set.add(stu8);
set.add(stu9);
System.out.println("集合中数据的个数:" + set.size());
for (Student st : set) {
System.out.println(st);
}
}
public static void main(String[] args) {
test();
}
}
运行结果:
集合中数据的个数:8
Student [name=aa, age=21]
Student [name=bb, age=21]
Student [name=aa, age=22]
Student [name=aa, age=23]
Student [name=dd, age=24]
Student [name=ff, age=24]
Student [name=ee, age=25]
Student [name=ee, age=26]<2>
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;
}
@Override
public String toString() {
return "Student [name=" + name + ", age=" + age + "]";
}
public Student() {
super();
}
public Student(String name, int age) {
super();
this.name = name;
this.age = age;
}
}
import java.util.Comparator;
//实现学生降序排列的比较器
public class StudentComparator1 implements Comparator<Student> {
@Override
public int compare(Student o1,Student o2) {
//根据第一个参数小于,等于或大于第二个参数分别返回负整数,零或正整数
if (o1.getAge() > o2.getAge()) {
return -1;
}else if (o1.getAge() < o2.getAge()) {
return 1;
}
if(o1.getName().compareTo(o2.getName()) > 0){
return -1;
}else if (o1.getName().compareTo(o2.getName()) < 0) {
return 1;
}
return 0;
}
}
import java.util.Comparator;
//实现学生升序排列的比较器
public class StudentComparator2 implements Comparator<Student> {
@Override
public int compare(Student o1,Student o2) {
//根据第一个参数小于,等于或大于第二个参数分别返回负整数,零或正整数
if (o1.getAge() > o2.getAge()) {
return 1;
}else if (o1.getAge() < o2.getAge()) {
return -1;
}
if(o1.getName().compareTo(o2.getName()) > 0){
return 1;
}else if (o1.getName().compareTo(o2.getName()) < 0) {
return -1;
}
return 0;
}
}
import java.util.Set;
import java.util.TreeSet;
//TreeSet可以保证添加到其中的String类型的数据不可重复且可以排序
public class TreeSetTest {
/*
* 我们要把Student类型的数据添加到TreeSet中,则Student要实现comparable接口并且要实现其中的compareTo()方法
* 在compareTo()方法中要实现Student类型的排序规则
* 当我们把Student类型的数据向TreeSet中添加的时候,TreeSet会自动的调用Student对象的compareTo()方法
* 如果compareTo()方法返回0,则不让数据进入集合;然后根据compareTo()方法返回负数/正数对象数据进行排序
*/
public static void test(){
//Set使用升序规则的比较器
Set<Student> set = new TreeSet<Student>(new StudentComparator1());
Student stu1 = new Student("aa",21);
Student stu2 = new Student("bb",21);
Student stu3 = new Student("aa",22);
Student stu4 = new Student("aa",23);
Student stu5 = new Student("dd",24);
Student stu6 = new Student("ee",25);
Student stu7 = new Student("ee",26);
Student stu8 = new Student("ff",24);
Student stu9 = new Student("aa",21);
set.add(stu1);
set.add(stu2);
set.add(stu3);
set.add(stu4);
set.add(stu5);
set.add(stu6);
set.add(stu7);
set.add(stu8);
set.add(stu9);
System.out.println("集合中数据的个数:" + set.size());
for (Student st : set) {
System.out.println(st);
}
}
public static void main(String[] args) {
test();
}
}
运行结果:
集合中数据的个数:8
Student [name=ee, age=26]
Student [name=ee, age=25]
Student [name=ff, age=24]
Student [name=dd, age=24]
Student [name=aa, age=23]
Student [name=aa, age=22]
Student [name=bb, age=21]
Student [name=aa, age=21]