相当于是set的孙子类
image.png
image.png
image.png
image.png

TreeSet集合:

image.png
image.png

返回值规则
image.png

  1. package com.itheima.d1_set;
  2. import java.util.Comparator;
  3. import java.util.Set;
  4. import java.util.TreeSet;
  5. /**
  6. * 目标: 观察TreeSet 对于有值特性的数据如何排序
  7. * 学会对自定义类型的对象进行指定规则排序
  8. */
  9. public class SetDemo4 {
  10. public static void main(String[] args) {
  11. // TreeSet 不重复,无索引 可排序
  12. TreeSet<Integer> sets = new TreeSet<>();
  13. sets.add(23);
  14. sets.add(24);
  15. sets.add(12);
  16. sets.add(8);
  17. System.out.println(sets);
  18. // TreeSet 不重复,无索引 可排序
  19. TreeSet<String> sets1 = new TreeSet<>();
  20. sets1.add("Java");
  21. sets1.add("Java");
  22. sets1.add("angela");
  23. sets1.add("黑马");
  24. sets1.add("Java");
  25. sets1.add("About");
  26. sets1.add("Python");
  27. sets1.add("UI");
  28. sets1.add("UI");
  29. // [About, Java, Python, UI, angela, 黑马]
  30. System.out.println(sets1); // TreeSet集合对于字符串是按照字母顺序排序的
  31. System.out.println("----------------------");
  32. // 方式二: 集合自带比较器对象进行规则定制 // 如果两种方式都有,默认用集合自带比较器对象的(方式二)
  33. Set<Apple> apples = new TreeSet<>(new Comparator<Apple>() {
  34. @Override
  35. public int compare(Apple o1, Apple o2) {
  36. // return o1.getWeight() - o2.getWeight(); // 默认第一个减第二个参数就是升序
  37. // return o2.getWeight() - o1.getWeight(); 降序
  38. // 像这种比较价格的,要用Double.compare的方法比较,避免小数相减失真
  39. return Double.compare(o2.getPrice(), o1.getPrice()); //降序
  40. }
  41. });
  42. // 创建TreeSet集合(用多态的形式,用父类接口Set接收)
  43. // Set<Apple> apples = new TreeSet<>();
  44. apples.add(new Apple("红富士","红色",9.9,500));
  45. apples.add(new Apple("青苹果","绿色",15.9,300));
  46. apples.add(new Apple("绿苹果","青色",29.9,400));
  47. apples.add(new Apple("黄苹果","红色",9.8, 500)); // 去掉重量重复的元素
  48. System.out.println(apples); // 自定义比较规则后,按照重量排序
  49. // [Apple{name='青苹果', color='绿色', price=15.9, weight=300}, Apple{name='绿苹果', color='青色', price=29.9, weight=400}, Apple{name='红富士', color='红色', price=9.9, weight=500}]
  50. }
  51. }
  1. package com.itheima.d1_set;
  2. // 使用Apple类 去实现接口的某种功能 // Comparable这里是比较功能的接口,传入Apple对象
  3. public class Apple implements Comparable<Apple>{
  4. private String name;
  5. private String color;
  6. private double price;
  7. private int weight;
  8. public Apple() {
  9. }
  10. public Apple(String name, String color, double price, int weight) {
  11. this.name = name;
  12. this.color = color;
  13. this.price = price;
  14. this.weight = weight;
  15. }
  16. public String getName() {
  17. return name;
  18. }
  19. public void setName(String name) {
  20. this.name = name;
  21. }
  22. public String getColor() {
  23. return color;
  24. }
  25. public void setColor(String color) {
  26. this.color = color;
  27. }
  28. public double getPrice() {
  29. return price;
  30. }
  31. public void setPrice(double price) {
  32. this.price = price;
  33. }
  34. public int getWeight() {
  35. return weight;
  36. }
  37. public void setWeight(int weight) {
  38. this.weight = weight;
  39. }
  40. // 写javabean现在一般会再加一个toString方法(重写),如果有引用类型可以直接打印其内容
  41. @Override
  42. public String toString() {
  43. return "Apple{" +
  44. "name='" + name + '\'' +
  45. ", color='" + color + '\'' +
  46. ", price=" + price +
  47. ", weight=" + weight +
  48. '}';
  49. }
  50. /**
  51. * 方式一: 类自定义比较规则
  52. * @param o the object to be compared.
  53. * @return
  54. */
  55. @Override
  56. public int compareTo(Apple o) {
  57. // 按照重量进行比较的
  58. // this代表你本身这个对象,o表示你放到参数里的对象 这里的this相当于o1 , o相当于o2
  59. // this是现在定义的这个类的学生对象,o是拿来比较输入的学生对象
  60. return this.weight - o.weight; // 升序排序
  61. // return this.weight - o.weight >= 0 ? 1 : -1; // 保留去重复的元素
  62. }
  63. }