鱼C论坛

 找回密码
 立即注册
查看: 2099|回复: 1

[已解决]Java在集合中使用泛型出现的问题

[复制链接]
发表于 2021-11-30 12:51:53 | 显示全部楼层 |阅读模式
10鱼币
本帖最后由 Twilight.. 于 2021-12-1 00:39 编辑

利用TreeSet实现Comparable、Comparator的两种排序方式(自然排序、定制排序)
QQ截图20211130124337.png
如果不采用泛型的话排序就正确了,采用泛型定制排序,结果发现排序不正确,请教大神们问题到底出在哪里了呢

不采用泛型:
  1. package collectionexer11_5;

  2. import java.util.Comparator;
  3. import java.util.Iterator;
  4. import java.util.TreeSet;


  5. public class TreeSetTest {
  6.     public static void main(String[] args) {
  7.         Employee e1 = new Employee("Tom", 22, new MyDate(1999,10,11));
  8.         Employee e2 = new Employee("Jerry", 20, new MyDate(2001,3,23));
  9.         Employee e3 = new Employee("Rose", 27, new MyDate(1994,5,1));
  10.         Employee e6 = new Employee("Rrose", 27, new MyDate(1927,5,1));
  11.         Employee e4 = new Employee("Jack", 22, new MyDate(1999,10,21));
  12.         Employee e5 = new Employee("Jim", 44, new MyDate(1977,12,6));

  13.         //2). 创建 TreeSet 时传入 Comparator 对象,按生日日期的先后排序。
  14.         System.out.println("\n创建 TreeSet 时传入 Comparator 对象,按生日日期的先后排序");
  15.         Comparator com = new Comparator() {
  16.             @Override
  17.             public int compare(Object o1, Object o2) {
  18.                 if(o1 instanceof Employee && o2 instanceof Employee){
  19.                     Employee e1 = (Employee) o1;
  20.                     Employee e2 = (Employee) o2;
  21.                     int year1 = e1.getBirthday().getYear();
  22.                     int year2 = e2.getBirthday().getYear();
  23.                     if(year1 < year2){
  24.                         return -1;
  25.                     }else if(year1 > year2){
  26.                         return 1;
  27.                     }else{
  28.                         int month1 = e1.getBirthday().getMonth();
  29.                         int month2 = e2.getBirthday().getMonth();
  30.                         if(month1 < month2){
  31.                             return -1;
  32.                         }else if(month1 > month2){
  33.                             return 1;
  34.                         }else{
  35.                             return Integer.compare(e1.getBirthday().getDay(), e2.getBirthday().getDay());
  36.                         }
  37.                     }
  38.                 }
  39.                 throw new RuntimeException("输入的参数类型有误");
  40.             }
  41.         };

  42.         TreeSet set2 = new TreeSet(com);
  43.         set2.add(e1);
  44.         set2.add(e2);
  45.         set2.add(e3);
  46.         set2.add(e4);
  47.         set2.add(e5);
  48.         set2.add(e6);
  49.         Iterator iterator2 = set2.iterator();
  50.         while(iterator2.hasNext()){
  51.             System.out.println(iterator2.next());
  52.         }

  53.     }
  54. }

  55. class Employee implements Comparable{
  56.     private String name;
  57.     private int age;
  58.     private MyDate birthday;

  59.     public Employee(String name, int age, MyDate birthday) {
  60.         this.name = name;
  61.         this.age = age;
  62.         this.birthday = birthday;
  63.     }

  64.     public Employee() {
  65.     }

  66.     public String getName() {
  67.         return name;
  68.     }

  69.     public void setName(String name) {
  70.         this.name = name;
  71.     }

  72.     public int getAge() {
  73.         return age;
  74.     }

  75.     public void setAge(int age) {
  76.         this.age = age;
  77.     }

  78.     public MyDate getBirthday() {
  79.         return birthday;
  80.     }

  81.     public void setBirthday(MyDate birthday) {
  82.         this.birthday = birthday;
  83.     }

  84.     @Override
  85.     public String toString() {
  86.         return "Employee{" +
  87.                 "name='" + name + '\'' +
  88.                 ", age=" + age +
  89.                 ", birthday=" + birthday +
  90.                 '}';
  91.     }

  92.     @Override
  93.     public int compareTo(Object obj){
  94.         if(obj instanceof Employee){
  95.             Employee e = (Employee)obj;
  96.             return this.name.compareTo(e.name);
  97.         }
  98.         throw new RuntimeException("输入的数据类型不一致");
  99.     }
  100. }

  101. class MyDate{
  102.     private int year;
  103.     private int month;
  104.     private int day;

  105.     public MyDate() {
  106.     }

  107.     public MyDate(int year, int month, int day) {
  108.         this.year = year;
  109.         this.month = month;
  110.         this.day = day;
  111.     }

  112.     public int getYear() {
  113.         return year;
  114.     }

  115.     public void setYear(int year) {
  116.         this.year = year;
  117.     }

  118.     public int getMonth() {
  119.         return month;
  120.     }

  121.     public void setMonth(int month) {
  122.         this.month = month;
  123.     }

  124.     public int getDay() {
  125.         return day;
  126.     }

  127.     public void setDay(int day) {
  128.         this.day = day;
  129.     }

  130.     @Override
  131.     public String toString() {
  132.         return "MyDate{" +
  133.                 "year=" + year +
  134.                 ", month=" + month +
  135.                 ", day=" + day +
  136.                 '}';
  137.     }
  138. }
复制代码

QQ截图20211130124619.png

采用泛型后:
  1. package genericdemo12_2;

  2. import org.junit.Test;

  3. import java.util.Comparator;
  4. import java.util.Iterator;
  5. import java.util.TreeSet;


  6. public class EmployeeTest {

  7.     //定制排序
  8.     @Test
  9.     public void testComparator(){
  10.         Comparator<Employee> com = new Comparator<Employee>() {
  11.             @Override
  12.             public int compare(Employee e1, Employee e2) {
  13. //                int year1 = e1.getBirthday().getYear();
  14. //                int year2 = e2.getBirthday().getYear();
  15. //                if (year1 < year2) {
  16. //                    return -1;
  17. //                } else if (year1 > year2) {
  18. //                    return 1;
  19. //                } else {
  20. //                    int month1 = e1.getBirthday().getMonth();
  21. //                    int month2 = e2.getBirthday().getMonth();
  22. //                    if (month1 < month2) {
  23. //                        return -1;
  24. //                    } else if (month1 > month2) {
  25. //                        return 1;
  26. //                    } else {
  27. //                        return Integer.compare(e1.getBirthday().getDay(), e2.getBirthday().getDay());
  28. //                    }
  29. //                }
  30.                 return e1.compareTo(e2);
  31.             }
  32.         };
  33.         TreeSet<Employee> set = new TreeSet<>(com);

  34.         Employee e1 = new Employee("Tom", 22, new MyDate(1999, 10, 11));
  35.         Employee e2 = new Employee("Jerry", 20, new MyDate(2001, 3, 23));
  36.         Employee e3 = new Employee("Rose", 27, new MyDate(1997, 3, 12));
  37.         Employee e6 = new Employee("RRose", 27, new MyDate(1927, 3, 12));
  38.         Employee e4 = new Employee("Jack", 22, new MyDate(1999, 10, 21));
  39.         Employee e5 = new Employee("Jim", 44, new MyDate(1977, 12, 6));

  40.         set.add(e1);
  41.         set.add(e2);
  42.         set.add(e3);
  43.         set.add(e4);
  44.         set.add(e5);
  45.         set.add(e6);

  46.         Iterator<Employee> iterator = set.iterator();
  47.         while(iterator.hasNext()) {
  48.             Employee e = iterator.next();
  49.             System.out.println(e);
  50.         }
  51.     }
  52. }
复制代码

微信截图_20211130124924.png
附employee和MyDate:
  1. package genericdemo12_2;

  2. /**
  3. * @author whoo
  4. * @create 2021-11-30 10:34
  5. */
  6. public class Employee implements Comparable<Employee>{
  7.     private String name;
  8.     private int age;
  9.     private MyDate birthday;

  10.     public Employee(String name, int age, MyDate birthday) {
  11.         this.name = name;
  12.         this.age = age;
  13.         this.birthday = birthday;
  14.     }

  15.     public Employee() {
  16.     }

  17.     public String getName() {
  18.         return name;
  19.     }

  20.     public void setName(String name) {
  21.         this.name = name;
  22.     }

  23.     public int getAge() {
  24.         return age;
  25.     }

  26.     public void setAge(int age) {
  27.         this.age = age;
  28.     }

  29.     public MyDate getBirthday() {
  30.         return birthday;
  31.     }

  32.     public void setBirthday(MyDate birthday) {
  33.         this.birthday = birthday;
  34.     }

  35.     @Override
  36.     public String toString() {
  37.         return "Employee{" +
  38.                 "name='" + name + '\'' +
  39.                 ", age=" + age +
  40.                 ", birihday=" + birthday +
  41.                 '}';
  42.     }

  43.     @Override
  44.     public int compareTo(Employee o) {
  45.         return this.name.compareTo(o.name);
  46.     }

  47. }
复制代码
  1. package genericdemo12_2;

  2. /**
  3. * @author whoo
  4. * @create 2021-11-30 10:35
  5. */
  6. public class MyDate implements Comparable<MyDate>{
  7.     private int month;
  8.     private int day;
  9.     private int year;

  10.     public MyDate(int month, int day, int year) {
  11.         this.month = month;
  12.         this.day = day;
  13.         this.year = year;
  14.     }

  15.     public MyDate() {

  16.     }

  17.     public int getMonth() {
  18.         return month;
  19.     }

  20.     public void setMonth(int month) {
  21.         this.month = month;
  22.     }

  23.     public int getDay() {
  24.         return day;
  25.     }

  26.     public void setDay(int day) {
  27.         this.day = day;
  28.     }

  29.     public int getYear() {
  30.         return year;
  31.     }

  32.     public void setYear(int year) {
  33.         this.year = year;
  34.     }

  35.     @Override
  36.     public String toString() {
  37.         return "{month=" + month +
  38.                 ", day=" + day +
  39.                 ", year=" + year + "}";
  40.     }

  41.     @Override
  42.     public int compareTo(MyDate o) {
  43.         if(this.year < o.year){
  44.             return -1;
  45.         }else if(this.year > o.year){
  46.             return 1;
  47.         }else{
  48.             if(this.month < o.month){
  49.                 return -1;
  50.             }else if(this.month > o.month){
  51.                 return 1;
  52.             }else{
  53.                 return Integer.compare(this.day, o.day);
  54.             }
  55.         }
  56.     }
  57. }
复制代码
最佳答案
2021-11-30 12:51:54
没问题啊,是不是你在MyDate的构造函数里写反了:
  1. public MyDate(int month, int day, int year)
复制代码

应改成
  1. public MyDate(int year, int month, int day)
复制代码

最佳答案

查看完整内容

没问题啊,是不是你在MyDate的构造函数里写反了: 应改成
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2021-11-30 12:51:54 | 显示全部楼层    本楼为最佳答案   
没问题啊,是不是你在MyDate的构造函数里写反了:
  1. public MyDate(int month, int day, int year)
复制代码

应改成
  1. public MyDate(int year, int month, int day)
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-4-27 04:58

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表