HashSet作业练习
案例一
-
定义一个Employee类,该类包含private成员属性name,age
1.创建三个Employee对象放入HashSet中
2.当name与age相同时,被认为是相同的员工,不能添加到HshSet中
package com.JiHe_.Set_;
import java.util.HashSet;
import java.util.Objects;
public class Employee_ {
public static void main(String[] args) {
HashSet hashSet = new HashSet();
hashSet.add(new Employee("玥骋",21));//加入成功
hashSet.add(new Employee("婉茹",20));//加入成功
hashSet.add(new Employee("玥骋",21));//加入不成功
System.out.println(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的值都相同则返回相同的hashcode哈希值
//hashcode判断在哪个链表,equals判断两个对象是否相等,如果相等是绝对加不进去的
@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);
}
}
案例二
-
定义一个Employee类,该类包含:private成员属性name,sal,birthday(MyDate类型),其中birthday为MyDate类型(属性包括,year,month,day)。注意MyDate类型需要自己定义
要求:
1.创建三个Employee放入HashSet中,
2.当name与birthday的值相同时,认为是相同元GG,不能添加到HashSet中
package com.JiHe_.Set_;
import java.util.HashSet;
import java.util.Objects;
public class Employee_ {
public static void main(String[] args) {
HashSet hashSet = new HashSet();
hashSet.add(new Employee("jack",new MyDate(2000,3,20)));
hashSet.add(new Employee("join",new MyDate(2000,10,30)));
hashSet.add(new Employee("jack",new MyDate(2000,3,20)));
System.out.println(hashSet);
}
}
/*
* 定义一个Employee类,该类包含:private成员属性name,sal,birthday(MyDate类型),其中birthday为MyDate类型(属性包括,year,month,day)。注意Mydata类型需要自己定义
要求:
1.创建三个Employee放入HashSet中,
2.当name与birthday的值相同时,认为是相同元GG,不能添加到HashSet中*/
class Employee{
private String name;
private MyDate birthday;
public Employee(String name,MyDate birthday){
this.name=name;
this.birthday=birthday;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public MyDate getBirthday() {
return birthday;
}
public void setBirthday(MyDate birthday) {
this.birthday = birthday;
}
@Override
public String toString() {
return "Employee{" +
"name='" + name + '\'' +
", 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);
}
}