• HashSet编程小案例,控制生日和姓名。重写HashCode


    Java编程

    定义员工Employee类,该类包含:private成员属性name,sal,birthday(MyDate类型),

    其中birthday为MyDate类型(属性包括:year,month,day),

    要求:

    1.创建3个Employee放入HashSet中;

    当name和birthday的值相同时,认为是相员工,不能添加到HashSet集合中。

     

    1. @SuppressWarnings({"all"})
    2. public static void main(String[] args) {
    3. HashSet hashset = new HashSet();
    4. Employeeday19 employee1 =new Employeeday19("Tom",11000,new MyDate(2010,12,12));
    5. Employeeday19 employee2 =new Employeeday19("Simth",18000,new MyDate(2011,12,18));
    6. Employeeday19 employee3 =new Employeeday19("Jenny",31000,new MyDate(2012,12,03));
    7. hashset.add(employee1);
    8. hashset.add(employee2);
    9. hashset.add(employee3);
    10. System.out.println(hashset);
    11. Employeeday19 employee4 =new Employeeday19("Jenny",31000,new MyDate(2012,12,03));
    12. hashset.add(employee4);
    13. System.out.println("=======二次打印=======");
    14. System.out.println(hashset);
    15. }
    16. }
    17. class MyDate {
    18. private int year;
    19. private int month;
    20. private int day;
    21. @Override
    22. public String toString() {
    23. return " year=" + year +
    24. ", month=" + month +
    25. ", day=" + day ;
    26. }
    27. @Override
    28. public boolean equals(Object o) {
    29. if (this == o) return true;
    30. if (o == null || getClass() != o.getClass()) return false;
    31. MyDate myDate = (MyDate) o;
    32. return year == myDate.year && month == myDate.month && day == myDate.day;
    33. }
    34. @Override
    35. public int hashCode() {
    36. return Objects.hash(year, month, day);
    37. }
    38. public int getYear() {
    39. return year;
    40. }
    41. public void setYear(int year) {
    42. this.year = year;
    43. }
    44. public int getMonth() {
    45. return month;
    46. }
    47. public void setMonth(int month) {
    48. this.month = month;
    49. }
    50. public int getDay() {
    51. return day;
    52. }
    53. public void setDay(int day) {
    54. this.day = day;
    55. }
    56. public MyDate(int year, int month, int day) {
    57. this.year = year;
    58. this.month = month;
    59. this.day = day;
    60. }
    61. }
    62. class Employeeday19 {
    63. private String name;
    64. private double sal;
    65. private MyDate birthday;
    66. @Override
    67. public boolean equals(Object o) {
    68. if (this == o) return true;
    69. if (o == null || getClass() != o.getClass()) return false;
    70. Employeeday19 that = (Employeeday19) o;
    71. return Objects.equals(name, that.name);
    72. }
    73. @Override
    74. public int hashCode() {
    75. return Objects.hash(name);
    76. }
    77. public String getName() {
    78. return name;
    79. }
    80. public void setName(String name) {
    81. this.name = name;
    82. }
    83. public double getSal() {
    84. return sal;
    85. }
    86. public void setSal(double sal) {
    87. this.sal = sal;
    88. }
    89. public MyDate getBirthday() {
    90. return birthday;
    91. }
    92. public void setBirthday(MyDate birthday) {
    93. this.birthday = birthday;
    94. }
    95. public Employeeday19(String name, double sal, MyDate birthday) {
    96. this.name = name;
    97. this.sal = sal;
    98. this.birthday = birthday;
    99. }
    100. @Override
    101. public String toString() {
    102. return "name='" + name + '\'' +
    103. ", sal=" + sal +
    104. ", birthday:" + birthday +"\n";
    105. }
  • 相关阅读:
    MCE | 表观遗传:YTHDF蛋白调节 m6A-RNA
    上海万粒携手花王中国 「贝贝粒妙妙宝宝学步助手」正式上线
    Python3.x安装Pandas教程
    如何审核 Active Directory 用户账户更改?
    C#.NET Framework 使用BC库(BouncyCastle) RSA 公钥加密 私钥解密 ver:20230706
    基因型数据VCF转EXCEL亲测好用
    解决:elementUI中el-dialog关闭后,再次打开保持初始化
    FullCalendarDemo5 控件的实例讲解—拖拽实现值班排班(五)
    《一个程序猿的生命周期》-《发展篇》- 42.逃离“管理”陷阱
    Selenium02
  • 原文地址:https://blog.csdn.net/qq_58341172/article/details/133913829