TreeSet无序,唯一
无参构造:需要在要添加的对象的类中 重写compareTo方法,实现Comparable接口
有参构造:参数为实现Comparator接口的子类对象
实现Comparator的子类MyComparator 重写compare方法,或直接传递 new Comparator(){} 匿名内部类
HashSet判断是否唯一通过hashCode()方法和equals()方法,引用类型如果对比值需要重写这两个方法
哈希表:是一个元素为链表的数组.综合了数组和链表的好处
LinkedHashSet:次鞥数据结构由哈希表和链表组成.
哈希表保证元素的唯一性
链表保证元素有序(存储和取出是一致的)
TreeSet 底层是二叉树结构(红黑树是之中自平衡的二叉树)
TreeSet 唯一排序
使用元素的自然顺序对元素进行排序
或者根据创建set时提供的Comparator进行排序
具体取决于构造方法
A: 自然排序: 使用无参构造创建TreeSet对象,需要对添加的引用类实现Comparable接口并重写compare方法.如下:
B: 比较器排序
自定义TreeSet集合按照先age再name排序
Student 类 必须实现 Comparable接口 需重写compare方法
public class Student implements Comparable {
private String name;
private int age;
public Student() {
super();
// TODO Auto-generated constructor stub
}
public Student(String name, int age) {
super();
this.name = name;
this.age = 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 int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + age;
result = prime * result + ((name == null) ? 0 : name.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Student other = (Student) obj;
if (age != other.age)
return false;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
return true;
}
@Override
public int compareTo(Student s) {
int num = this.age - s.age;
int num2 = num == 0 ? this.name.compareTo(s.name) : num;
return num2;
}
}
按照name的长度为主排序的compare方法
@Override
public int compareTo(Student s) {
int num = this.name.length() - s.name.length();
int num1 = num == 0 ? this.name.compareTo(s.name) : num;
int num2 = num1 == 0 ? this.age - s.age : num1;
return num2;
}
测试类
import java.util.TreeSet;
public class TreeSetTest {
public static void main(String[] args) {
TreeSet ts = new TreeSet();
Student s = new Student(“张柏芝”,24);
Student s1 = new Student(“张柏芝”,20);
Student s2 = new Student(“张柏芝”,16);
Student s3 = new Student(“张柏芝”,17);
Student s4 = new Student(“张柏芝”,23);
Student s5 = new Student(“张柏芝”,26);
Student s6 = new Student(“任逍遥”,26);
ts.add(s);
ts.add(s1);
ts.add(s2);
ts.add(s3);
ts.add(s4);
ts.add(s5);
ts.add(s6);
for(Student sd:ts) {
System.out.println(sd.getName()+sd.getAge());
}
}
}
-----------------------------使用匿名内部类实现TreeSet创建带参数构造方法----------------------------------
import java.util.Comparator;
import java.util.TreeSet;
public class TreeTest {
public static void main(String[] args) {
TreeSet trs = new TreeSet(new Comparator() {
public int compare(Student s1, Student s2) {
int sum = s1.getName().length()-s2.getName().length();
int sum1 = sum==0? s1.getName().compareTo(s2.getName()):sum;
int sum2 = sum1==0? s1.getAge()-s2.getAge():sum1;
return sum2;
}
});
Student s = new Student("张柏芝",28);
Student s1 = new Student("张柏",19);
Student s2 = new Student("张芝",19);
Student s3 = new Student("张柏芝",20);
Student s4 = new Student("张柏芝",26);
Student s5 = new Student("芝",27);
Student s6 = new Student("张柏芝",23);
Student s7 = new Student("张芝",25);
trs.add(s);
trs.add(s1);
trs.add(s2);
trs.add(s3);
trs.add(s4);
trs.add(s5);
trs.add(s6);
trs.add(s7);
for(Student sd:trs) {
System.out.println(sd.getAge()+sd.getName());
}
}
}
-----------------------------TreeSet创建带参数构造方法----创建实现Comparator接口子类------------------------------
import java.util.Comparator;
public class MyComparator implements Comparator{
@Override
public int compare(Student s1, Student s2) {
int sum = s1.getName().length()-s2.getName().length();
int sum1 = sum==0? s1.getName().compareTo(s2.getName()):sum;
int sum2 = sum1==0? s1.getAge()-s2.getAge():sum1;
return sum2;
}
}
import java.util.Comparator;
import java.util.TreeSet;
public class TreeTest {
public static void main(String[] args) {
TreeSet trs = new TreeSet(new MyComparator());
Student s = new Student(“张柏芝”,28);
Student s1 = new Student(“张柏”,19);
Student s2 = new Student(“张芝”,19);
Student s3 = new Student(“张柏芝”,20);
Student s4 = new Student(“张柏芝”,26);
Student s5 = new Student(“芝”,27);
Student s6 = new Student(“张柏芝”,23);
Student s7 = new Student(“张芝”,25);
trs.add(s);
trs.add(s1);
trs.add(s2);
trs.add(s3);
trs.add(s4);
trs.add(s5);
trs.add(s6);
trs.add(s7);
for(Student sd:trs) {
System.out.println(sd.getAge()+sd.getName());
}
}