package edu.wtbu;
import java.util.Comparator;
import java.util.Iterator;
import java.util.TreeSet;
public class Demo01 {
public static void main(String[] args) {
//TreeSet:存储结构:红黑树
//要求:元素必须实现Comparable接口,compareTo(); 方法的返回值为0,认为是重复元素
//创建集合
TreeSet<String> treeSet = new TreeSet<>();
//1.添加元素
treeSet.add("xyz");
treeSet.add("abc");
treeSet.add("hello");
treeSet.add("xyz");//重复,不再添加
System.out.println("元素个数:"+treeSet.size());//元素个数:3
System.out.println(treeSet);//[abc, hello, xyz]
//2.删除元素
treeSet.remove("xyz");
System.out.println("元素个数:"+treeSet.size());//元素个数:2
System.out.println(treeSet);//[abc, hello]
//3.遍历
//1.使用增强for
for (String s:
treeSet) {
System.out.println(s);
}
//2.使用迭代器
Iterator<String> iterator = treeSet.iterator();
while (iterator.hasNext()){
System.out.println(iterator.next());
}
//4.判断
System.out.println(treeSet.contains("abc"));//true
System.out.println(treeSet.isEmpty());//false
System.out.println("==============================");
TreeSet<Student> treeSet1 = new TreeSet<>();
//1.添加元素
Student s1 = new Student("张三", 16);
Student s2 = new Student("李四", 17);
Student s3 = new Student("王五", 18);
Student s4 = new Student("王五", 19);
Student s5 = new Student("王五", 19);
treeSet1.add(s1);
treeSet1.add(s2);
treeSet1.add(s3);
treeSet1.add(s4);
treeSet1.add(s5);//重复,不在添加
System.out.println("元素个数:"+treeSet1.size());//元素个数:4
System.out.println(treeSet1);//[Student{name='张三', age=16}, Student{name='李四', age=17}, Student{name='王五', age=18}, Student{name='王五', age=19}]
//2.删除元素
//treeSet1.remove(s1);
treeSet1.remove(new Student("张三", 16));
System.out.println("元素个数:"+treeSet1.size());//元素个数:3
System.out.println(treeSet1);//[Student{name='李四', age=17}, Student{name='王五', age=18}, Student{name='王五', age=19}]
//3.遍历
//1.使用增强for
for (Student s:
treeSet1) {
System.out.println(s);
}
//2.使用迭代器
Iterator<Student> iterator1 = treeSet1.iterator();
while (iterator1.hasNext()){
System.out.println(iterator1.next());
}
//4.判断
System.out.println(treeSet1.contains(s2));//true
System.out.println(treeSet1.isEmpty());//false
System.out.println("=========================");
//Comparator接口:实现定制比较
TreeSet<Student> treeSet2 = new TreeSet<>(new Comparator<Student>() {
@Override
public int compare(Student o1, Student o2) {
int n1 = o1.getAge() - o2.getAge();
int n2 = o1.getName().compareTo(o2.getName());
return n1==0?n2:n1;
}
});
Student s8 = new Student("张三", 15);
Student s9 = new Student("李四", 17);
Student s10 = new Student("王五", 18);
treeSet2.add(s8);
treeSet2.add(s9);
treeSet2.add(s10);
System.out.println("元素个数:"+treeSet2.size());//元素个数:4
System.out.println(treeSet2);//[Student{name='张三', age=16}, Student{name='李四', age=17}, Student{name='王五', age=18}, Student{name='王五', age=19}]
//Comparator接口的应用:使用TreeSet集合实现字符串按照长度进行排序
//创建集合,并指定比较规则
TreeSet<String> treeSet3 = new TreeSet<>(new Comparator<String>() {
@Override
public int compare(String o1, String o2) {
int n1 = o1.length() - o2.length();
int n2 = o1.compareTo(o2);
return n1==0?n2:n1;
}
});
treeSet3.add("hubei");
treeSet3.add("beijing");
treeSet3.add("shanghai");
treeSet3.add("tianjin");
treeSet3.add("hunan");
treeSet3.add("shandong");
System.out.println(treeSet3);//[hubei, hunan, beijing, tianjin, shandong, shanghai]
}
}
package edu.wtbu;标签:name,Comparator,age,System,接口,Student,println,out,TreeSet From: https://www.cnblogs.com/123456dh/p/17137043.html
import java.util.Objects;
public class Student implements Comparable<Student>{
private String name;
private int age;
public Student() {
}
public Student(String name, int age) {
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 String toString() {
return "Student{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
// @Override
// public int hashCode() {
// int n1 = this.name.hashCode();
// int n2 = this.age;
// return n1+n2;
// }
//
// @Override
// public boolean equals(Object obj) {
// if(obj==this){
// return false;
// }
// if(obj==null){
// return false;
// }
// if(obj instanceof Student){
// Student s = (Student) obj;
// if(this.name.equals(s.getName())&&this.age==s.getAge()){
// return true;
// }
// }
// return false;
// }
//快捷键:Alt+insert+equals and (hashCode)
// @Override
// public boolean equals(Object o) {
// if (this == o) return true;
// if (o == null || getClass() != o.getClass()) return false;
// Student student = (Student) o;
// return age == student.age && Objects.equals(name, student.name);
// }
//
// @Override
// public int hashCode() {
// return Objects.hash(name, age);
// }
//先按照姓名比,再按照年龄比
@Override
public int compareTo(Student o) {
int n1 = this.getName().compareTo(o.getName());
int n2 = this.age - o.getAge();
return n1==0?n2:n1;
}
}