![](/img/trans.png)
[英]NoSuchElementException using Iterator class with HashSet
[英]Using HashSet with a user class Employee
我知道這聽起來是一個非常愚蠢的問題,但是在我對HashSet
了解以及我在代碼下面執行時所看到的內容后,我感到困惑。
我有一個Employee類如下(只保留相關的代碼段):
public class Employee {
//assume it has 3 variable name(String),salary(double) and id(int)
//assume the constructor, getter-setters are there
//following is my equals and hashCode implementation
public boolean equals(Employee e){
return name.equals(e.name);
}
public int hashCode(){
return id;
}
}
現在我有以下使用HashSet
代碼:
Employee e1 = new Employee("Abc", 2.0, 1);
Employee e2 = new Employee("abc", 3.0, 4);
Employee e3 = new Employee("XYZ", 4.0, 3);
Employee e4 = new Employee("Mno", 5.0, 2);
Employee e5 = new Employee("Abc", 77.0, 1);
Set<Employee> sEmp = new HashSet<Employee>();
sEmp.add(e1);
sEmp.add(e2);
sEmp.add(e3);
sEmp.add(e4);
sEmp.add(e5);
for(Employee e : sEmp){
System.out.println(e);
}
所以我將所有對象數據打印在我的控制台上:
Abc 77.0 1
Abc 2.0 1
Mno 5.0 2
XYZ 4.0 3
abc 3.0 4
AFAIK,該套裝不允許重復,這個重復將檢查equals
(如果我錯了,請糾正我)。
此外, HashSet
使用hashCode
,因此在上面的情況下,它不應該添加對象e5
。 但它成功地將該元素添加到集合中。 這困惑了我。
(如果我錯過了標准和所有這些東西,請忽略,我試圖理解概念/實現)。
編輯:這聽起來可能是一個愚蠢的問題,但我正在准備認證,並試圖看看這些東西是如何工作的。
你正在超載equals
而不是覆蓋它。 其參數應為Object
類型。
但是你的hashCode
正在檢查id
而equals
正在檢查name
。 它們應該由相同的屬性構成。
這是我們應該盡可能使用@Override
注釋的標准示例。
如果你將這個注釋與你的equals
方法一起使用,編譯器會通知你沒有重寫equals
方法,因為在超類中沒有equals(Employe)
方法,但是equals(Object)
。 所以你重載了這個方法(你正在用不同的參數創建額外的方法)。
因為這個HashSet不使用你的equals
方法的代碼,而是來自Object
類的equals(Object)
方法的代碼, equals(Object)
檢查引用相等:
public boolean equals(Object obj) {
return (this == obj);
}
我在equals方法中添加了一些代碼。 這將允許您更新員工ID的最新值。
package com.test.day16;
import java.util.HashSet;
import java.util.Set;
/**
*
* @author PradeepPadmarajaiah
*
*/
public class Employee {
private int empId;
private String empName;
public Employee(int empId, String empName) {
super();
this.empId = empId;
this.empName = empName;
}
/**
* @return the empId
*/
public final int getEmpId() {
return empId;
}
/**
* @param empId
* the empId to set
*/
public final void setEmpId(int empId) {
this.empId = empId;
}
/**
* @return the empName
*/
public final String getEmpName() {
return empName;
}
/**
* @param empName
* the empName to set
*/
public final void setEmpName(String empName) {
this.empName = empName;
}
/*
* (non-Javadoc)
*
* @see java.lang.Object#toString()
*/
@Override
public String toString() {
return "Employee [empId=" + empId + ", empName=" + empName + "]";
}
@Override
public int hashCode() {
return this.empId;
}
@Override
public boolean equals(Object obj) {
Employee employee = (Employee) obj;
if (employee.empId == this.empId) {
employee.setEmpName(this.empName);
return true;
} else {
return false;
}
}
public static void main(String[] args) {
Set<Employee> employees = new HashSet<>();
employees.add(new Employee(1, "Raj"));
employees.add(new Employee(1, "Pradeep"));
employees.add(new Employee(1, "Kumar"));
employees.add(new Employee(2, "Chandan"));
employees.add(new Employee(2, "Amitava"));
System.out.println(employees);
}
}
檢查“ employee.setEmpName(this.empName); ”行,這將覆蓋id的值。 這意味着,empId 1將具有最新的empName值作為Kumar Else,empId 1將empName值作為Raj首先被分配,並且在檢查此工作之后它將不會將值傳遞給Kumar 。現在類似於HashMap機制。
代碼結果將是 [Employee [empId = 1,empName = Kumar],Employee [empId = 2,empName = Amitava]]
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.