• Day10-Java进阶-泛型&数据结构(树)&TreeSet 集合


    1. 泛型

    1.1 泛型介绍

    1. package com.itheima.generics;
    2. import java.util.ArrayList;
    3. import java.util.Iterator;
    4. public class GenericsDemo1 {
    5. /*
    6. 泛型介绍 : JDK5引入的, 可以在编译阶段约束操作的数据类型, 并进行检查
    7. 注意 : 泛型默认的类型是Object, 且只能接引用数据类型
    8. 泛型的好处:
    9. 1. 统一数据类型
    10. 2. 将运行期的错误提升到了编译期
    11. 泛型的学习路径:
    12. 1. 泛型类
    13. 2. 泛型方法
    14. 3. 泛型接口
    15. 4. 泛型通配符
    16. 5. 泛型的限定
    17. */
    18. public static void main(String[] args) {
    19. ArrayList list = new ArrayList<>();
    20. list.add("张三");
    21. list.add("李四");
    22. list.add("王五");
    23. Iterator it = list.iterator();
    24. while (it.hasNext()) {
    25. Object o = it.next();
    26. String s = (String) o;
    27. System.out.println(s.length());
    28. }
    29. }
    30. }

    1.2 泛型类

    1. package com.itheima.generics;
    2. import java.util.ArrayList;
    3. public class GenericsDemo2 {
    4. /*
    5. 常见的泛型标识符 : E V K T
    6. E : Element
    7. T : Type
    8. K : Key(键)
    9. V : Value(值)
    10. 清楚不同的泛型, 在什么时机能确定到具体的类型
    11. 泛型类 : 创建对象的时候
    12. */
    13. public static void main(String[] args) {
    14. Student stu = new Student<>();
    15. }
    16. }
    17. class Student {
    18. private E e;
    19. public E getE() {
    20. return e;
    21. }
    22. public void setE(E e) {
    23. this.e = e;
    24. }
    25. }

    1.3 泛型方法

    1. package com.itheima.generics;
    2. public class GenericsDemo3 {
    3. /*
    4. 泛型方法
    5. 1. 非静态的方法 : 内部的泛型, 会根据类的泛型去匹配
    6. 2. 静态的方法 : 静态方法中如果加入了泛型, 必须声明出自己独立的泛型
    7. - 时机: 在调用方法, 传入实际参数的时候, 确定到具体的类型
    8. */
    9. public static void main(String[] args) {
    10. String[] arr1 = {"张三", "李四", "王五"};
    11. Integer[] arr2 = {11, 22, 33};
    12. Double[] arr3 = {11.1, 22.2, 33.3};
    13. printArray(arr1);
    14. printArray(arr2);
    15. printArray(arr3);
    16. }
    17. public static void printArray(T[] arr) {
    18. System.out.print("[");
    19. for (int i = 0; i < arr.length - 1; i++) {
    20. System.out.print(arr[i] + ", ");
    21. }
    22. System.out.println(arr[arr.length - 1] + "]");
    23. }
    24. }

    1.4 泛型接口

    1. package com.itheima.generics;
    2. import java.util.ArrayList;
    3. import java.util.List;
    4. public class GenericsDemo4 {
    5. /*
    6. 泛型接口
    7. 1. 实现类, 实现接口的时候确定到具体的泛型
    8. 2. 实现类实现接口, 没有指定具体类型, 就让接口的泛型, 跟着类的泛型去匹配
    9. */
    10. public static void main(String[] args) {
    11. InterBIml i = new InterBIml<>();
    12. }
    13. }
    14. interface Inter {
    15. void show(E s);
    16. }
    17. class InterAIml implements Inter{
    18. @Override
    19. public void show(String s) {
    20. }
    21. }
    22. class InterBIml implements Inter{
    23. @Override
    24. public void show(E e) {
    25. }
    26. }

    1.5 泛型通配符

    1. package com.itheima.generics;
    2. import java.util.ArrayList;
    3. public class GenericsDemo5 {
    4. /*
    5. 泛型通配符
    6. ? : 任意类型
    7. ? extends E : 可以传入的是E, 或者是E的子类
    8. ? super E : 可以传入的是E, 或者是E的父类
    9. */
    10. public static void main(String[] args) {
    11. ArrayList list1 = new ArrayList<>();
    12. list1.add(new Coder());
    13. ArrayList list2 = new ArrayList<>();
    14. list2.add(new Manager());
    15. ArrayList list3 = new ArrayList<>();
    16. list3.add("abc");
    17. ArrayList list4 = new ArrayList<>();
    18. list4.add("aaa");
    19. //method(list1); // 编译错误
    20. //method(list2); // 编译错误
    21. //method(list3); // 编译错误
    22. method(list4); // 编译正常
    23. }
    24. // 可以传入Employee及其子类
    25. public static void method(ArrayListsuper Employee> list){
    26. for (Object o : list) {
    27. Employee e = (Employee) o;
    28. e.work();
    29. }
    30. }
    31. }
    32. abstract class Employee{
    33. private String name;
    34. private double salary;
    35. public Employee() {
    36. }
    37. public Employee(String name, double salary) {
    38. this.name = name;
    39. this.salary = salary;
    40. }
    41. public abstract void work();
    42. /**
    43. * 获取
    44. * @return name
    45. */
    46. public String getName() {
    47. return name;
    48. }
    49. /**
    50. * 设置
    51. * @param name
    52. */
    53. public void setName(String name) {
    54. this.name = name;
    55. }
    56. /**
    57. * 获取
    58. * @return salary
    59. */
    60. public double getSalary() {
    61. return salary;
    62. }
    63. /**
    64. * 设置
    65. * @param salary
    66. */
    67. public void setSalary(double salary) {
    68. this.salary = salary;
    69. }
    70. public String toString() {
    71. return "Employee{name = " + name + ", salary = " + salary + "}";
    72. }
    73. }
    74. class Coder extends Employee{
    75. @Override
    76. public void work() {
    77. System.out.println("程序员写代码...");
    78. }
    79. }
    80. class Manager extends Employee{
    81. @Override
    82. public void work() {
    83. System.out.println("项目经理分配任务...");
    84. }
    85. }
    86. 2. 数据结构(树)

      2.1 树的介绍

      2.1 平衡二叉树

      2.1.1 左旋

      2.1.2 右旋

      2.1.3 左左旋

      2.1.4 左右旋

      2.1.5 右右旋

      2.1.6 右左旋

      2.2 红黑树

      当每次添加节点的颜色是黑色

      当每次添加节点的颜色是红色

      3. TreeSet 集合

      3.1 TreeSet 集合元素排序介绍

      1. package com.itheima.set;
      2. import java.util.TreeSet;
      3. public class TreeSetDemo1 {
      4. /*
      5. TreeSet集合的特点体验 : 排序, 去重
      6. */
      7. public static void main(String[] args) {
      8. TreeSet ts = new TreeSet<>();
      9. ts.add("a");
      10. ts.add("d");
      11. ts.add("e");
      12. ts.add("c");
      13. ts.add("b");
      14. ts.add("b");
      15. ts.add("b");
      16. System.out.println(ts);
      17. }
      18. }

      以下以返回值全为 -1 (倒序排序为例)

      3.2 TreeSet 排序 (自然排序)

      1. package com.itheima.domain;
      2. public class Student implements Comparable{
      3. // this.xxx - o.xxx 正序
      4. // o.xxx - this.xxx 降序
      5. @Override
      6. public int compareTo(Student o) {
      7. // 根据年龄做主要排序条件
      8. int ageResult = this.age - o.age;
      9. // 根据姓名做次要排序条件
      10. int nameResult = ageResult == 0 ? o.name.compareTo(this.name) : ageResult;
      11. // 判断姓名是否相同
      12. int result = nameResult == 0 ? 1 : nameResult;
      13. return result;
      14. }
      15. private String name;
      16. private int age;
      17. public Student() {
      18. }
      19. public Student(String name, int age) {
      20. this.name = name;
      21. this.age = age;
      22. }
      23. /**
      24. * 获取
      25. * @return name
      26. */
      27. public String getName() {
      28. return name;
      29. }
      30. /**
      31. * 设置
      32. * @param name
      33. */
      34. public void setName(String name) {
      35. this.name = name;
      36. }
      37. /**
      38. * 获取
      39. * @return age
      40. */
      41. public int getAge() {
      42. return age;
      43. }
      44. /**
      45. * 设置
      46. * @param age
      47. */
      48. public void setAge(int age) {
      49. this.age = age;
      50. }
      51. public String toString() {
      52. return "Student{name = " + name + ", age = " + age + "}";
      53. }
      54. }
      1. package com.itheima.set;
      2. import com.itheima.domain.Student;
      3. import java.util.TreeSet;
      4. public class TreeSetDemo2 {
      5. /*
      6. TreeSet集合存储Student学生对象
      7. compareTo 方法的返回值 :
      8. 0 : 只有王五,25
      9. 1 : 正序排列
      10. -1 : 倒序排序
      11. */
      12. public static void main(String[] args) {
      13. TreeSet ts = new TreeSet();
      14. ts.add(new Student("王五", 25));
      15. ts.add(new Student("王五", 25));
      16. ts.add(new Student("王五", 25));
      17. ts.add(new Student("王五", 25));
      18. System.out.println(ts);
      19. }
      20. }

      3.3 TreeSet 排序 (比较器排序)

      1. package com.itheima.set;
      2. import com.itheima.domain.Student;
      3. import java.util.Comparator;
      4. import java.util.TreeSet;
      5. public class TreeSetDemo3 {
      6. /*
      7. 如果同时具备比较器和自然排序, 会优先按照比较器的规则, 进行排序操作.
      8. */
      9. public static void main(String[] args) {
      10. TreeSet ts = new TreeSet(new Comparator() {
      11. @Override
      12. public int compare(Student o1, Student o2) {
      13. int ageResult = o1.getAge() - o2.getAge();
      14. return ageResult == 0 ? o1.getName().compareTo(o2.getName()) : ageResult;
      15. }
      16. });
      17. ts.add(new Student("赵六", 26));
      18. ts.add(new Student("李四", 24));
      19. ts.add(new Student("张三", 23));
      20. ts.add(new Student("王五", 25));
      21. System.out.println(ts);
      22. }
      23. }

      1. package com.itheima.set;
      2. import java.util.Comparator;
      3. import java.util.TreeSet;
      4. public class TreeSetDemo4 {
      5. public static void main(String[] args) {
      6. TreeSet ts = new TreeSet<>(new Comparator() {
      7. @Override
      8. public int compare(String o1, String o2) {
      9. return o2.length() - o1.length();
      10. }
      11. });
      12. ts.add("aa");
      13. ts.add("aaaaaaaa");
      14. ts.add("aaa");
      15. ts.add("a");
      16. System.out.println(ts);
      17. }
      18. }

    87. 相关阅读:
      【sklearn | 3】时间序列分析与自然语言处理
      玩转数据可视化之R语言ggplot2:(十)坐标轴和刻度线设置1
      大话设计模式之抽象工厂模式
      arcgis删除细长图斑的方法
      使用Python进行食品配送时间预测
      父类和子类
      stack和queue简单实现(容器适配器)
      基于nodejs+vue校园失物招领平台设计与实现
      揭秘LLM计算数字的障碍的底层原理
      Docker 常用命令使用
    88. 原文地址:https://blog.csdn.net/weixin_68063226/article/details/137374680