练习——hashset简单练习

发布时间 2023-04-07 16:06:54作者: Q1uuuu
package com.collection_.set_;

import java.util.HashSet;
import java.util.Objects;

public class HashSetExercise {
    public static void main(String[] args) {
        /*
        定义一个Employee类,该类包含private成员属性name , age要求:
        创建3个Employee 对象放入 HashSet中
        当name利lage的值相同时,认为是相同员工,不能添加到HashSet集合中

        HashSet HashSet = new HashSet();
        HashSet.add(new Employee("lihua",18));
        HashSet.add(new Employee("john",28));
        HashSet.add(new Employee("lihua",18));

        System.out.println(HashSet);
    */

        /*
        定义一个Employee类,该类包含:private成员属性name,sal,birthday(MyDate类型),
        其中 birthday 为 MyDate类型(属性包括:year, month, day),要求:
            1.创建3个Employee 放入 HashSet中
            2.当name和birthday的值相同时,认为是相同员工,不能添加到HashSet集合中
         */

        HashSet hashSet = new HashSet();
        hashSet.add(new Employee("lihua",12000,new Mydate(2001,1,5)));
        hashSet.add(new Employee("zhouqi",12000,new Mydate(2001,1,5)));
        hashSet.add(new Employee("zhouqi",12000,new Mydate(2001,2,5)));
        hashSet.add(new Employee("zhouqi",11000,new Mydate(2001,1,5)));
        hashSet.add(new Employee("lihua",12000,new Mydate(2001,1,5)));

        System.out.println("hashset" + hashSet);
    }
}


/*class Employee{
    private String name;
    private int age;

    public Employee(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

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

    public int getAge() {
        return age;
    }

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

    @Override
    public String toString() {
        return "Employee{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }


    //如果name和age相同,则返回相同的hash值

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Employee employee = (Employee) o;
        return age == employee.age && Objects.equals(name, employee.name);
    }

    @Override
    public int hashCode() {
        return Objects.hash(name, age);
    }
}
*/
class Employee{
    private String name;
    private int sal;
    private Mydate birthday;

    public Employee(String name, int sal, Mydate birthday) {
        this.name = name;
        this.sal = sal;
        this.birthday = birthday;
    }

    public String getName() {
        return name;
    }

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

    public int getSal() {
        return sal;
    }

    public void setSal(int sal) {
        this.sal = sal;
    }

    public Mydate getBirthday() {
        return birthday;
    }

    public void setBirthday(Mydate birthday) {
        this.birthday = birthday;
    }

    @Override
    public String toString() {
        return "Employee{" +
                "name='" + name + '\'' +
                ", sal=" + sal +
                ", birthday=" + birthday +
                '}';
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Employee employee = (Employee) o;
        return Objects.equals(name, employee.name) && Objects.equals(birthday, employee.birthday);
    }

    @Override
    public int hashCode() {
        return Objects.hash(name, birthday);
    }
}

class Mydate{
    private int year;
    private int month;
    private int day;

    public Mydate(int year, int month, int day) {
        this.year = year;
        this.month = month;
        this.day = day;
    }

    public int getYear() {
        return year;
    }

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

    public int getMonth() {
        return month;
    }

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

    public int getDay() {
        return day;
    }

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

    @Override
    public String toString() {
        return "Mydate{" +
                "year=" + year +
                ", month=" + month +
                ", day=" + day +
                '}';
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Mydate mydate = (Mydate) o;
        return year == mydate.year && month == mydate.month && day == mydate.day;
    }

    @Override
    public int hashCode() {
        return Objects.hash(year, month, day);
    }
}