文章目录
1. 集合类总纲
2. TreeSet
2.1 TreeSet 性质
- TreeSet 是一个有序的Set 集合,可以按一定的规则指定元素的顺序。
- 不能有重复元素。
- 对象重写 equals() 和 hashcode() 失效。
- 无序不重复
2.2 元素有序
2.2.1 使用默认排序
package javaweb.collection;
import java.util.Iterator;
import java.util.TreeSet;
public class MyTreeSet {
public static void practice1() {
TreeSet<String> treeset = new TreeSet<String>();
treeset.add("a");
treeset.add("c");
treeset.add("e");
treeset.add("f");
treeset.add("b");
Iterator<String> it = treeset.iterator();
while(it.hasNext()) {
System.out.print(it.next() + " ");
}
}
public static void main(String[] args) {
practice1();
}
}
a b c e f
2.2.2 自定义排序
2.2.2.1 自己构造比较器
package javaweb.collection;
import java.util.Comparator;
import java.util.Iterator;
import java.util.TreeSet;
/**
* 自定义排序,降序
*
* @author NoBug
* @version 1.0
* @time 2022/3/16
* @Blog https://blog.youkuaiyun.com/qq_51184516
*
*/
class MyCmp implements Comparator<Object> {
@Override
public int compare(Object obj1, Object obj2) {
int x = 0;
Student s1 = (Student) obj1;
Student s2 = (Student) obj2;
if (s1.getAge() > s2.getAge())
x = -1;
else if (s1.getAge() < s2.getAge())
x = 1;
else
x = s1.getId().compareTo(s2.getId()); // 前者大于后者,返回1
return x;
}
}
public class MyTreeSet {
public static void main(String[] args) {
TreeSet<Student> treeset = new TreeSet<Student>(new MyCmp());
Student s1 = new Student("1001", "chen", 18);
Student s2 = new Student("1001", "luo", 25);
Student s3 = new Student("1003", "zhang", 25);
Student s4 = new Student("1004", "pi", 20);
Student s5 = new Student("1002", "wang", 25);
treeset.add(s1);
treeset.add(s2);
treeset.add(s3);
treeset.add(s4);
treeset.add(s5);
Iterator<Student> it = treeset.iterator();
while (it.hasNext()) {
System.out.println(it.next());
}
}
}
按年龄降序,相同年龄,按学号升序
1001 luo 25
1002 wang 25
1003 zhang 25
1004 pi 20
1001 chen 18
附件:Studen类
package javaweb.collection;
import java.util.HashSet;
import java.util.Iterator;
/**
* 测试不重复
*
* @author NoBug
* @version 1.0
* @time 2022/3/16
* @Blog https://blog.youkuaiyun.com/qq_51184516
*
*/
public class Student {
String id, name;
int age;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public Student(String id, String name, int age) {
this.id = id;
this.name = name;
this.age = age;
}
@Override
public String toString() {
return id + " " + name + " " + age;
}
@Override
public boolean equals(Object obj) {
return id.equals(((Student) obj).id);
}
@Override
public int hashCode() {
return Integer.valueOf(id);
}
public static void main(String[] args) {
HashSet<Student> hashset = new HashSet<Student>();
Student student1 = new Student("1001", "chen", 18);
Student student2 = new Student("1001", "chen", 18);
hashset.add(student1);
hashset.add(student2);
Iterator<Student> it = hashset.iterator();
while (it.hasNext()) {
System.out.println(it.next() + " ");
}
}
}
2.2.2.2 在 Student类里面就重写 compareTo()
package javaweb.collection;
import java.util.Iterator;
import java.util.TreeSet;
public class Student1 implements Comparable<Object> {
String id, name;
int age;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public Student1(String id, String name, int age) {
this.id = id;
this.name = name;
this.age = age;
}
@Override
public String toString() {
return id + " " + name + " " + age;
}
@Override
public int compareTo(Object o) {
int x = 0;
Student1 s2 = (Student1) o;
if (getAge() > s2.getAge())
x = -1;
else if (getAge() < s2.getAge())
x = 1;
else
x = getId().compareTo(s2.getId()); // 前者大于后者,返回1
return x;
}
public static void main(String[] args) {
TreeSet<Student1> treeset = new TreeSet<Student1>();
Student1 s1 = new Student1("1001", "chen", 18);
Student1 s2 = new Student1("1001", "luo", 25);
Student1 s3 = new Student1("1003", "zhang", 25);
Student1 s4 = new Student1("1004", "pi", 20);
Student1 s5 = new Student1("1002", "wang", 25);
treeset.add(s1);
treeset.add(s2);
treeset.add(s3);
treeset.add(s4);
treeset.add(s5);
Iterator<Student1> it = treeset.iterator();
while (it.hasNext()) {
System.out.println(it.next());
}
}
}
1001 luo 25
1002 wang 25
1003 zhang 25
1004 pi 20
1001 chen 18