public class TreeSet<E>
extends AbstractSet<E>
implements NavigableSet<E>, Cloneable, Serializable
基于 TreeMap
的 NavigableSet
实现。使用元素的自然顺序对元素进行排序,或者根据创建 set 时提供的 Comparator
进行排序,具体取决于使用的构造方法。
// 迭代其实我们可以简单地理解为遍历,是一个标准化遍历各类容器里面的所有对象的方法类,
// 它是一个很典型的设计模式。Iterator模式是用于遍历集合类的标准访问方法。
// 它可以把访问逻辑从不同类型的集合类中抽象出来,从而避免向客户端暴露集合的内部结构。
import java.util.Iterator;
import java.util.TreeSet;
public class TreeSetDemo {
/**
* 放入的字符串,按照字符串的自然顺序排序,字符串的自然顺序就是字典顺序
*/
public static void testMethod() {
TreeSet treeset = new TreeSet();
treeset.add("was");
treeset.add("db2");
treeset.add("java");
// boolean add(E e) 将指定的元素添加到此 set(如果该元素尚未存在于 set 中)。
Iterator iterator = treeset.iterator();
while (iterator.hasNext()) {
System.out.println(iterator.next());
}
}
/**
* 向TreeSet放入自定义对象 自定义类要实现Comparable接口
*/
public static void testMethod2() {
TreeSet treeset = new TreeSet();
Employee one = new Employee("zs", 22);
Employee two = new Employee("ww", 18);
Employee three = new Employee("lisi", 26);
treeset.add(one);
treeset.add(two);
treeset.add(three);
Iterator iterator = treeset.iterator();
while (iterator.hasNext()) {
Employee emp = (Employee) iterator.next();
System.out.println(emp.getName() + "==" + emp.getAge());
}
}
/**
* 向TreeSet放入自定义对象 产生集合时给其传递排序规则
*/
public static void testMethod3() {
TreeSet set = new TreeSet(new ComparatorDemo());
Employee one = new Employee("zs", 22);
Employee two = new Employee("ww", 18);
Employee three = new Employee("lisi", 26);
set.add(one);
set.add(two);
set.add(three);
Iterator iterator = set.iterator();
while (iterator.hasNext()) {
Employee emp = (Employee) iterator.next();
System.out.println(emp.getName() + "==" + emp.getAge());
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
testMethod2();
}
}
public class Employee implements Comparable {
private String name;
private int age;
public int compareTo(Object o) {
// TODO Auto-generated method stub
Employee emp = (Employee) o;
return emp.age - this.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;
}
public Employee() {
super();
// TODO Auto-generated constructor stub
}
public Employee(String name, int age) {
super();
this.name = name;
this.age = age;
}
public String toString() {
return this.name + "==" + this.age;
}
}
import java.util.Comparator;
import java.util.function.Function;
import java.util.function.ToDoubleFunction;
import java.util.function.ToIntFunction;
import java.util.function.ToLongFunction;
public class ComparatorDemo implements Comparator {
// public int compare(Object o1, Object o2) {
// // TODO Auto-generated method stub
// Employee one =(Employee)o1;
// Employee two =(Employee)o2;
// return one.getAge()-two.getAge();
// }
@Override
public int compare(Object o1, Object o2) {
// TODO Auto-generated method stub
return 0;
}
public Comparator reversed() {
// TODO Auto-generated method stub
return null;
}
public Comparator thenComparing(Comparator other) {
// TODO Auto-generated method stub
return null;
}
public Comparator thenComparing(Function keyExtractor,
Comparator keyComparator) {
// TODO Auto-generated method stub
return null;
}
public Comparator thenComparing(Function keyExtractor) {
// TODO Auto-generated method stub
return null;
}
public Comparator thenComparingInt(ToIntFunction keyExtractor) {
// TODO Auto-generated method stub
return null;
}
public Comparator thenComparingLong(ToLongFunction keyExtractor) {
// TODO Auto-generated method stub
return null;
}
public Comparator thenComparingDouble(ToDoubleFunction keyExtractor) {
// TODO Auto-generated method stub
return null;
}
public static <T extends Comparable<? super T>> Comparator<T> reverseOrder() {
// TODO Auto-generated method stub
return null;
}
public static <T extends Comparable<? super T>> Comparator<T> naturalOrder() {
// TODO Auto-generated method stub
return null;
}
public static <T> Comparator<T> nullsFirst(Comparator<? super T> comparator) {
// TODO Auto-generated method stub
return null;
}
public static <T> Comparator<T> nullsLast(Comparator<? super T> comparator) {
// TODO Auto-generated method stub
return null;
}
public static <T, U> Comparator<T> comparing(
Function<? super T, ? extends U> keyExtractor,
Comparator<? super U> keyComparator) {
// TODO Auto-generated method stub
return null;
}
public static <T, U extends Comparable<? super U>> Comparator<T> comparing(
Function<? super T, ? extends U> keyExtractor) {
// TODO Auto-generated method stub
return null;
}
public static <T> Comparator<T> comparingInt(
ToIntFunction<? super T> keyExtractor) {
// TODO Auto-generated method stub
return null;
}
public static <T> Comparator<T> comparingLong(
ToLongFunction<? super T> keyExtractor) {
// TODO Auto-generated method stub
return null;
}
public static <T> Comparator<T> comparingDouble(
ToDoubleFunction<? super T> keyExtractor) {
// TODO Auto-generated method stub
return null;
}
}