• 泛型的使用案例,以及年月日的定制排序,传入Comparator对象


     

    简易版:传送门:过关展将之——birthday排序(年月日排序)-CSDN博客  

     实现过程;

    1. public static void main(String[] args) {
    2. ArrayList arrayList = new ArrayList<>();
    3. Employee grace = new Employee("Grace", 12000, new MyDate(2012, 12, 12));
    4. Employee tom = new Employee("Tom", 28000, new MyDate(2001, 12, 03));
    5. Employee david = new Employee("David", 30000, new MyDate(1999, 10, 10));
    6. Employee david1 = new Employee("David", 31000, new MyDate(2001, 11, 15));
    7. Employee david2 = new Employee("David", 32000, new MyDate(1899, 5, 14));
    8. Employee david3 = new Employee("David", 33000, new MyDate(2013, 04, 11));
    9. Employee david4 = new Employee("David", 34000, new MyDate(1987, 04, 10));
    10. Employee david5 = new Employee("David", 34000, new MyDate(1987, 04, 11));
    11. arrayList.add(grace);
    12. arrayList.add(tom);
    13. arrayList.add(david);
    14. arrayList.add(david1);
    15. arrayList.add(david2);
    16. arrayList.add(david3);
    17. arrayList.add(david4);
    18. arrayList.add(david5);
    19. // System.out.println("打印先前数据:" + arrayList);
    20. //现在进行排序
    21. arrayList.sort(new Comparator() {
    22. @Override
    23. public int compare(Employee o1, Employee o2) {
    24. if (!(o1.getName() != null && o2.getName() != null)) {
    25. throw new RuntimeException("员工姓名不能为空!");
    26. }else{
    27. if (o1.getName().equals(o2.getName())) {
    28. if (o1.getBirthday().getYear() == o2.getBirthday().getYear()) {
    29. if (o1.getBirthday().getMonth() == o2.getBirthday().getMonth()) {
    30. if (o1.getBirthday().getDay() == o2.getBirthday().getDay()) {
    31. return 0;
    32. } else if (o1.getBirthday().getDay() > o2.getBirthday().getDay()) {
    33. return 1;
    34. } else {
    35. return -1;
    36. }
    37. } else if (o1.getBirthday().getMonth() > o2.getBirthday().getMonth()) {
    38. return 1;
    39. } else {
    40. return -1;
    41. }
    42. } else if (o1.getBirthday().getYear() > o2.getBirthday().getYear()) {
    43. return 1;
    44. } else {
    45. return -1;
    46. }
    47. } else {
    48. return o1.getName().compareTo(o2.getName());
    49. }
    50. }
    51. }
    52. });
    53. System.out.println("========排序后======");
    54. for (Object o :arrayList) {
    55. Employee employee = (Employee) o;
    56. System.out.println(employee);
    57. }
    58. }
    59. }
    60. class MyDate {
    61. private int year;
    62. private int month;
    63. private int day;
    64. public MyDate(int year, int month, int day) {
    65. this.year = year;
    66. this.month = month;
    67. this.day = day;
    68. }
    69. public int getYear() {
    70. return year;
    71. }
    72. public void setYear(int year) {
    73. this.year = year;
    74. }
    75. public int getMonth() {
    76. return month;
    77. }
    78. public void setMonth(int month) {
    79. this.month = month;
    80. }
    81. public int getDay() {
    82. return day;
    83. }
    84. public void setDay(int day) {
    85. this.day = day;
    86. }
    87. @Override
    88. public String toString() {
    89. return "MyDate{" +
    90. "year=" + year +
    91. ", month=" + month +
    92. ", day=" + day +
    93. '}';
    94. }
    95. }
    96. class Employee {
    97. private String name;
    98. @Override
    99. public String toString() {
    100. return "Employee{" +
    101. "name='" + name + '\'' +
    102. ", sal=" + sal +
    103. ", birthday=" + birthday +
    104. '}';
    105. }
    106. private double sal;
    107. public String getName() {
    108. return name;
    109. }
    110. public void setName(String name) {
    111. this.name = name;
    112. }
    113. public double getSal() {
    114. return sal;
    115. }
    116. public void setSal(double sal) {
    117. this.sal = sal;
    118. }
    119. public MyDate getBirthday() {
    120. return birthday;
    121. }
    122. public void setBirthday(MyDate birthday) {
    123. this.birthday = birthday;
    124. }
    125. private MyDate birthday;
    126. public Employee(String name, double sal, MyDate birthday) {
    127. this.name = name;
    128. this.sal = sal;
    129. this.birthday = birthday;
    130. }
    131. }

     以上代码过于繁琐,使用 if 的嵌套,可读性不高,现在进行改良:

    进一步优化过程:

    1. @SuppressWarnings({"all"})
    2. public static void main(String[] args) {
    3. ArrayList arrayList = new ArrayList<>();
    4. EmployeeImp grace = new EmployeeImp("Grace", 12000, new MyDateImp(2012, 12, 12));
    5. EmployeeImp tom = new EmployeeImp("Tom", 28000, new MyDateImp(2001, 12, 03));
    6. EmployeeImp david = new EmployeeImp("David", 30000, new MyDateImp(1999, 10, 10));
    7. EmployeeImp david1 = new EmployeeImp("David", 31000, new MyDateImp(2001, 11, 15));
    8. EmployeeImp david2 = new EmployeeImp("David", 32000, new MyDateImp(1899, 5, 14));
    9. EmployeeImp david3 = new EmployeeImp("David", 33000, new MyDateImp(2013, 04, 11));
    10. EmployeeImp david4 = new EmployeeImp("David", 34000, new MyDateImp(1987, 04, 10));
    11. EmployeeImp david5 = new EmployeeImp("David", 34000, new MyDateImp(1987, 04, 11));
    12. arrayList.add(grace);
    13. arrayList.add(tom);
    14. arrayList.add(david);
    15. arrayList.add(david1);
    16. arrayList.add(david2);
    17. arrayList.add(david3);
    18. arrayList.add(david4);
    19. arrayList.add(david5);
    20. //下面开始订制排序
    21. arrayList.sort(new Comparator() {
    22. @Override
    23. public int compare(EmployeeImp o1, EmployeeImp o2) {
    24. if (!(o1 instanceof EmployeeImp && o2 instanceof EmployeeImp)) {
    25. System.out.println("类型不匹配");
    26. return 0;
    27. }
    28. //比较name
    29. int i = o1.getName().compareTo(o2.getName());
    30. if (i==0) {//如果名字相同,就继续比较
    31. int yearMinus =o1.getBirthday().getYear() - o2.getBirthday().getYear();
    32. if(yearMinus==0){//如果两个年份相同,继续比较月份
    33. int monthMinus = o1.getBirthday().getMonth()- o2.getBirthday().getMonth();
    34. if(monthMinus==0){//如果两个月份相同,继续比较日
    35. int dayMinus = o1.getBirthday().getDay()- o2.getBirthday().getDay();
    36. if(dayMinus==0){
    37. return 0;
    38. }else{
    39. return dayMinus;
    40. }
    41. }else{
    42. return monthMinus;
    43. }
    44. }else{
    45. return yearMinus;
    46. }
    47. } else{//如果名字不相同,就返回,出结果
    48. return i;
    49. }
    50. }
    51. });
    52. System.out.println("======定制排序后=====");
    53. for (Object o :arrayList) {
    54. EmployeeImp employeeImp = (EmployeeImp) o;
    55. System.out.println(employeeImp);
    56. }
    57. }
    58. }
    59. class MyDateImp {
    60. private int year;
    61. private int month;
    62. private int day;
    63. public MyDateImp(int year, int month, int day) {
    64. this.year = year;
    65. this.month = month;
    66. this.day = day;
    67. }
    68. public int getYear() {
    69. return year;
    70. }
    71. public void setYear(int year) {
    72. this.year = year;
    73. }
    74. public int getMonth() {
    75. return month;
    76. }
    77. public void setMonth(int month) {
    78. this.month = month;
    79. }
    80. public int getDay() {
    81. return day;
    82. }
    83. public void setDay(int day) {
    84. this.day = day;
    85. }
    86. @Override
    87. public String toString() {
    88. return "MyDate{" +
    89. "year=" + year +
    90. ", month=" + month +
    91. ", day=" + day +
    92. '}';
    93. }
    94. }
    95. class EmployeeImp {
    96. private String name;
    97. @Override
    98. public String toString() {
    99. return "Employee{" +
    100. "name='" + name + '\'' +
    101. ", sal=" + sal +
    102. ", birthday=" + birthday +
    103. '}';
    104. }
    105. private double sal;
    106. public String getName() {
    107. return name;
    108. }
    109. public void setName(String name) {
    110. this.name = name;
    111. }
    112. public double getSal() {
    113. return sal;
    114. }
    115. public void setSal(double sal) {
    116. this.sal = sal;
    117. }
    118. public MyDateImp getBirthday() {
    119. return birthday;
    120. }
    121. public void setBirthday(MyDateImp birthday) {
    122. this.birthday = birthday;
    123. }
    124. private MyDateImp birthday;
    125. public EmployeeImp(String name, double sal, MyDateImp birthday) {
    126. this.name = name;
    127. this.sal = sal;
    128. this.birthday = birthday;
    129. }
    130. }

    使用过关斩将法改良:

    1. public static void main(String[] args) {
    2. ArrayList arrayList = new ArrayList<>();
    3. EmployeeImp grace = new EmployeeImp("Grace", 12000, new MyDateImp(2012, 12, 12));
    4. EmployeeImp tom = new EmployeeImp("Tom", 28000, new MyDateImp(2001, 12, 03));
    5. EmployeeImp david = new EmployeeImp("David", 30000, new MyDateImp(1999, 10, 10));
    6. EmployeeImp david1 = new EmployeeImp("David", 31000, new MyDateImp(2001, 11, 15));
    7. EmployeeImp david2 = new EmployeeImp("David", 32000, new MyDateImp(1899, 5, 14));
    8. EmployeeImp david3 = new EmployeeImp("David", 33000, new MyDateImp(2013, 04, 11));
    9. EmployeeImp david4 = new EmployeeImp("David", 34000, new MyDateImp(1987, 04, 10));
    10. EmployeeImp david5 = new EmployeeImp("David", 34000, new MyDateImp(1987, 04, 11));
    11. arrayList.add(grace);
    12. arrayList.add(tom);
    13. arrayList.add(david);
    14. arrayList.add(david1);
    15. arrayList.add(david2);
    16. arrayList.add(david3);
    17. arrayList.add(david4);
    18. arrayList.add(david5);
    19. //下面开始订制排序
    20. arrayList.sort(new Comparator() {
    21. @Override
    22. public int compare(EmployeeImp o1, EmployeeImp o2) {
    23. if (!(o1 instanceof EmployeeImp && o2 instanceof EmployeeImp)) {
    24. System.out.println("类型不匹配");
    25. return 0;
    26. }
    27. //比较name
    28. int i = o1.getName().compareTo(o2.getName());
    29. if (i != 0) {//如果名字不相同,直接返回
    30. return i;
    31. }
    32. //以下是对birthday的比较,因此,我们最好把这个比较,放在MyDate
    33. int yearMinus = o1.getBirthday().getYear() - o2.getBirthday().getYear();
    34. if (yearMinus != 0) {//如果两个年份不相同,直接返回
    35. return yearMinus;
    36. }
    37. int monthMinus = o1.getBirthday().getMonth() - o2.getBirthday().getMonth();
    38. if (monthMinus != 0) {//如果两个月份不相同,直接返回
    39. return monthMinus;
    40. }
    41. //如果year、month都相同
    42. return o1.getBirthday().getDay() - o2.getBirthday().getDay();
    43. }
    44. });
    45. System.out.println("======定制排序后=====");
    46. for (
    47. Object o : arrayList) {
    48. EmployeeImp employeeImp = (EmployeeImp) o;
    49. System.out.println(employeeImp);
    50. }
    51. }
    52. }
    53. class MyDateImp {
    54. private int year;
    55. private int month;
    56. private int day;
    57. public MyDateImp(int year, int month, int day) {
    58. this.year = year;
    59. this.month = month;
    60. this.day = day;
    61. }
    62. public int getYear() {
    63. return year;
    64. }
    65. public void setYear(int year) {
    66. this.year = year;
    67. }
    68. public int getMonth() {
    69. return month;
    70. }
    71. public void setMonth(int month) {
    72. this.month = month;
    73. }
    74. public int getDay() {
    75. return day;
    76. }
    77. public void setDay(int day) {
    78. this.day = day;
    79. }
    80. @Override
    81. public String toString() {
    82. return "MyDate{" +
    83. "year=" + year +
    84. ", month=" + month +
    85. ", day=" + day +
    86. '}';
    87. }
    88. }
    89. class EmployeeImp {
    90. private String name;
    91. @Override
    92. public String toString() {
    93. return "Employee{" +
    94. "name='" + name + '\'' +
    95. ", sal=" + sal +
    96. ", birthday=" + birthday +
    97. '}';
    98. }
    99. private double sal;
    100. public String getName() {
    101. return name;
    102. }
    103. public void setName(String name) {
    104. this.name = name;
    105. }
    106. public double getSal() {
    107. return sal;
    108. }
    109. public void setSal(double sal) {
    110. this.sal = sal;
    111. }
    112. public MyDateImp getBirthday() {
    113. return birthday;
    114. }
    115. public void setBirthday(MyDateImp birthday) {
    116. this.birthday = birthday;
    117. }
    118. private MyDateImp birthday;
    119. public EmployeeImp(String name, double sal, MyDateImp birthday) {
    120. this.name = name;
    121. this.sal = sal;
    122. this.birthday = birthday;
    123. }
    124. }

     

    打印结果:

    简易版:传送门:过关展将之——birthday排序(年月日排序)-CSDN博客 

  • 相关阅读:
    DeepSort目标跟踪算法
    国家网络安全周 | 保障智能网联汽车产业,护航汽车数据安全
    软件外包开发设计文档
    优秀的 Verilog/FPGA开源项目介绍(三十九)- NVMe
    物联网主机:为智能交通赋能
    Docker学习笔记
    肖sir__设计测试用例方法之边界值03_(黑盒测试)
    计算机毕业设计springboot+vue基本微信小程序的校园二手物品交易平台系统
    深入理解 Python 虚拟机:字典(dict)的优化
    Cy3-PEG-maleimide,Cy3-聚乙二醇-马来酰亚胺,MAL-PEG-Cy3
  • 原文地址:https://blog.csdn.net/qq_58341172/article/details/133982935