TreeSet是Set的一个实现类,它内部采用自平衡的二叉树来存储元素(左<中<右),集合没有重复的元素,可以对元素进行排序。排序有两种方法:
1.自然排序:实现Comparable接口,重写compareTo()方法
2.比较器排序:实现Comparator,重写compare()方法,利用其实例对象作为TreeSet的比较器。
一、自然排序:
package study;
import java.util.*;
class Student3 implements Comparable{
String name;
int age;
public Student3(String name, int age) {
this.name = name;
this.age = age;
}
@Override
public String toString() {
return name+":"+age;
}
@Override
public int compareTo(Object obj) {
// TODO Auto-generated method stub
Student3 stu=(Student3)obj;
if(this.age>stu.age){
return 1;
}
if(this.age==stu.age){
return this.name.compareTo(stu.name);
}
return -1;
}
}
public class Example27 {
public static void main(String[] args) {
TreeSet ts=new TreeSet();
ts.add(new Student3("Jack", 19));
ts.add(new Student3("Rose",18));
ts.add(new Student3("Tom", 19));
ts.add(new Student3("Rose", 18));
Iterator it=ts.iterator();
while(it.hasNext()){
System.out.println(it.next());
}
}
}
import java.util.*;
class Student3 implements Comparable{
String name;
int age;
public Student3(String name, int age) {
this.name = name;
this.age = age;
}
@Override
public String toString() {
return name+":"+age;
}
@Override
public int compareTo(Object obj) {
// TODO Auto-generated method stub
Student3 stu=(Student3)obj;
if(this.age>stu.age){
return 1;
}
if(this.age==stu.age){
return this.name.compareTo(stu.name);
}
return -1;
}
}
public class Example27 {
public static void main(String[] args) {
TreeSet ts=new TreeSet();
ts.add(new Student3("Jack", 19));
ts.add(new Student3("Rose",18));
ts.add(new Student3("Tom", 19));
ts.add(new Student3("Rose", 18));
Iterator it=ts.iterator();
while(it.hasNext()){
System.out.println(it.next());
}
}
}
结果:
自然排序在元素内部重写compareTo()方法,通过比较元素返回-1、0或者1,当age相同时,再用compare()比较name那一段代码理解得不是特别清晰,是不是因为String类中就已经重写了compare()方法,所以就可以直接使用compare()对name进行比较,而对自己定义的类需要重写compare()方法,即使是在重写的compare()方法中,使用compare(String a)方法时调用的依旧是String中重写的compare()方法?
二、
比较器排序
package study;
import java.util.Comparator;
import java.util.Iterator;
import java.util.TreeSet;
class MyComparator implements Comparator{
@Override
public int compare(Object obj1, Object obj2) {
// TODO Auto-generated method stub
String st1=(String)obj1;
String st2=(String)obj2;
int temp=st1.length()-st2.length();
return temp;
}
}
public class Example28 {
public static void main(String[] args) {
TreeSet ts=new TreeSet(new MyComparator());
ts.add("Jack");
ts.add("Helena");
ts.add("Eve");
Iterator it=ts.iterator();
while(it.hasNext()){
System.out.println(it.next());
}
}
}
import java.util.Comparator;
import java.util.Iterator;
import java.util.TreeSet;
class MyComparator implements Comparator{
@Override
public int compare(Object obj1, Object obj2) {
// TODO Auto-generated method stub
String st1=(String)obj1;
String st2=(String)obj2;
int temp=st1.length()-st2.length();
return temp;
}
}
public class Example28 {
public static void main(String[] args) {
TreeSet ts=new TreeSet(new MyComparator());
ts.add("Jack");
ts.add("Helena");
ts.add("Eve");
Iterator it=ts.iterator();
while(it.hasNext()){
System.out.println(it.next());
}
}
}
结果: