TreeSet同样实现了Set接口,例外还实现SortedSet接口,从而实现了:
既可以去重,又可以排序

特点


1、它内部的元素在存放时,有一定的存储顺序
2、它同样不能存放重复元素
3、TreeSet 底层是TreeMap结构,去重原理:如同HashSet,同样要比较Hash值,以及equals()

扩展方法


​TreeSet - 图1

用法


  1. import java.util.Iterator;
  2. import java.util.Set;
  3. import java.util.TreeSet;
  4. public class TreeSetStudy {
  5. public static void main(String[] args) {
  6. // TODO Auto-generated method stub
  7. //定义TreeSet的集合
  8. TreeSet<String> data = new TreeSet<String>();
  9. data.add("a");
  10. data.add("c");
  11. data.add("a");
  12. data.add("d");
  13. data.add("b");
  14. //得到集合长度
  15. System.out.println(data.size());
  16. //判断集合中是否包含某一个元素
  17. System.out.println(data.contains("a"));
  18. //判断集合是否为空
  19. System.out.println(data.isEmpty());
  20. // data.remove("a");
  21. //取得第1个元素
  22. System.out.println(data.first());
  23. //取得最后1个元素
  24. System.out.println(data.last());
  25. //严格返回比“b” 大的,该集合中的最小元素,如果没有此元素则返回 null (比我大的,但是又是最靠近我的)
  26. System.out.println(data.higher("b"));
  27. //返回比我小的,也是最靠近我的
  28. System.out.println(data.lower("b"));
  29. //取得集合中,比我小的所有元素
  30. System.out.println(data.headSet("c"));
  31. //取得集合中,比我大的所有元素(但是包含自己)
  32. System.out.println(data.tailSet("b"));
  33. //遍历(1)foreach
  34. // for (String string : data) {
  35. // System.out.println(string);
  36. // }
  37. //遍历(2)迭代器
  38. // Iterator<String> iterator = data.iterator();
  39. // while(iterator.hasNext()) {
  40. //
  41. // String str = iterator.next();
  42. //
  43. // System.out.println(str);
  44. // }
  45. }
  46. }

TreeSet排序规则


自然排序


在JDK类库中,有一部分类实现了Comparable接口,例如Integer、Double、String等等。Comparable接口在java.lang包中,该接口有一个compareTo(Object o)方法,返回整型数据。对于表达式x.compareTo(y),如果返回值为0,则表示x和y相等;如果返回值大于0,则表示x大于y;如果返回值小于0,则表示x小于y。TreeSet集合调用对象的compareTo()方法比较集合中的大小,然后进行升序排列,这种方式称为自然排序

如果元素要参与自然排序,需要实现一个接口Comparable

白话:凡是元素自带了排序规则的,都是属于自然排序
​TreeSet - 图2
​TreeSet - 图3

  1. import java.io.Serializable;
  2. public class StudentBean implements Serializable,Comparable<StudentBean> {
  3. /**
  4. *
  5. */
  6. private static final long serialVersionUID = -2640219895840760729L;
  7. private String name;
  8. private int age;
  9. public StudentBean(String name, int age) {
  10. super();
  11. this.name = name;
  12. this.age = age;
  13. }
  14. public StudentBean() {
  15. super();
  16. // TODO Auto-generated constructor stub
  17. }
  18. public String getName() {
  19. return name;
  20. }
  21. public void setName(String name) {
  22. this.name = name;
  23. }
  24. public int getAge() {
  25. return age;
  26. }
  27. public void setAge(int age) {
  28. this.age = age;
  29. }
  30. @Override
  31. public int hashCode() {
  32. final int prime = 31;
  33. int result = 1;
  34. result = prime * result + age;
  35. result = prime * result + ((name == null) ? 0 : name.hashCode());
  36. return result;
  37. }
  38. @Override
  39. public boolean equals(Object obj) {
  40. if (this == obj)
  41. return true;
  42. if (obj == null)
  43. return false;
  44. if (getClass() != obj.getClass())
  45. return false;
  46. StudentBean other = (StudentBean) obj;
  47. if (age != other.age)
  48. return false;
  49. if (name == null) {
  50. if (other.name != null)
  51. return false;
  52. } else if (!name.equals(other.name))
  53. return false;
  54. return true;
  55. }
  56. /**
  57. * 定义排序规则
  58. */
  59. @Override
  60. public int compareTo(StudentBean o) {
  61. // TODO Auto-generated method stub
  62. //自定义排序规则(0 ---我们地位是一样的 整数 ---你的地位比我大
  63. //负数 --- 你的地位比我小)
  64. return this.name.hashCode() - o.name.hashCode();
  65. }
  66. }
  1. private static void study03() {
  2. // TODO Auto-generated method stub
  3. TreeSet<StudentBean> data = new TreeSet<StudentBean>();
  4. StudentBean stu01 = new StudentBean("张三",18);
  5. StudentBean stu02 = new StudentBean("李四",15);
  6. StudentBean stu03 = new StudentBean("王五",23);
  7. data.add(stu01);
  8. data.add(stu02);
  9. data.add(stu03);
  10. System.out.println(data);
  11. }

自定义排序


自定义排序 和自然排序 都是用来集合完成排序的,区别在于:
自然排序,需要在元素身上实现Comparable
而自定义排序 ,需要程序员自己制定 排序比较器

  1. import java.util.Comparator;
  2. import com.woniuxy.java21.bean.StudentBean;
  3. /**
  4. * 自定义比较器,用来完成集合排序
  5. * @author Administrator
  6. *
  7. */
  8. public class InitStudentComparator implements Comparator<StudentBean>{
  9. /***
  10. * 0 我们地位相当
  11. * 正数 你的地位比我大 应该靠后
  12. * 负数 你的地位比我低 应该靠前
  13. */
  14. @Override
  15. public int compare(StudentBean o1, StudentBean o2) {
  16. // TODO Auto-generated method stub
  17. //升序规则
  18. return o1.getAge() - o2.getAge();
  19. }
  20. }
  1. private static void study03() {
  2. // TODO Auto-generated method stub
  3. TreeSet<StudentBean> data = new TreeSet<StudentBean>(new InitStudentComparator());
  4. StudentBean stu01 = new StudentBean("张三",18);
  5. StudentBean stu02 = new StudentBean("李四",15);
  6. StudentBean stu03 = new StudentBean("王五",23);
  7. data.add(stu01);
  8. data.add(stu02);
  9. data.add(stu03);
  10. System.out.println(data);
  11. }