gpt4 book ai didi

java - Hashmap、排序/重复问题

转载 作者:行者123 更新时间:2023-12-01 13:55:48 25 4
gpt4 key购买 nike

好吧,我还需要解决一个问题。我的 HashMap 代码添加/删除/修改/打印/排序(某种程度上)。这是排序问题。它按姓氏升序排序。如果两个员工的姓氏相同,则按名字升序排列。但问题是,如果两个员工的名字也相同,那么它必须按 ID 号排序,但事实并非如此。

我的代码:如果两个员工的全名相同,但身份证号码不同,那么它只打印出两个员工中身份证号码较小的员工,并从列表中忽略另一个员工。问题是,我该如何修复它,以及是否必须修复此代码我的员工文件?这是我的员工代码。任何提示/链接/建议都会有帮助。

public class Employee implements Comparable {
private String firstName;
private String lastName;
private int id;
private int perfScale;

Employee() {
firstName = "";
lastName = "";
id = 0;
perfScale = 0;
}

Employee(String lastName, String firstName, int id, int perfScale){
this.firstName = firstName;
this.lastName = lastName;
this.id = id;
this.perfScale = perfScale;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName){
this.lastName = lastName;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName){
this.firstName = firstName;
}
public int getId() {
return id;
}
public void setId(int id){
this.id = id;
}
public int getPerfScale() {
return perfScale;
}
public void setPerfScale(int perfScale){
this.perfScale = perfScale;
}
public boolean equals(Object o) {
if(o instanceof Employee)
return(getLastName() == ((Employee) o) .getLastName()) &&
(getFirstName() == ((Employee)o) .getFirstName());
else
return false;
}

public int compareTo(Object o) {
Employee e = (Employee) o;
int performance1 = e.getPerfScale();
int performance2 = this.getPerfScale();

if(performance1 < performance2) {
return 1;

} else if(performance1 > performance2) {
return -1;
} else {
return this.getLastName().compareTo(e.getLastName());
}
}

public int hashCode() {
int h1 = firstName.hashCode();
int h2 = lastName.hashCode();
int h3 = new Integer(id).hashCode();
final int HASH_MULTIPLIER1 = 29;
final int HASH_MULTIPLIER2 = 19;
final int HASH_MULTIPLIER3 = 17;
int h = HASH_MULTIPLIER1 * h1 + HASH_MULTIPLIER2 * h2 + HASH_MULTIPLIER3 * h3;
return h;
}

public String toString()
{
return getLastName() + ", " + getFirstName() + " ," + getId() + " rating: " + getPerfScale()+ " Performance Scale";

}
}

现在这只是我处理按升序名称排序的情况的代码。整个代码比我打印的代码长,所以我只打印它产生排序的情况。

public static void printLastNameAscending(TreeMap<String, Employee> LastName,
TreeMap<Integer, Employee>idNumber) {
Set Employee1 = LastName.entrySet();
Set Employee2 = idNumber.entrySet();
Iterator itr1 = Employee1.iterator();
Iterator itr2 = Employee2.iterator();

while (itr1.hasNext() && itr2.hasNext()) {
Map.Entry me = (Map.Entry) itr1.next();
Map.Entry be = (Map.Entry) itr2.next();
System.out.print(me.getKey()+ " ID: ");
System.out.println(be.getKey());
}

}

最佳答案

The problem though, is if the two employees has the same first name as well, then it has to sort by ID number, which does not.

您的 compareTo 不会对 ID 号和名字执行任何操作。事实上,它首先使用的是性能。

My code: If two employee have the same full name but different ID number, then it only prints out the employee who's ID number is lesser between the two, and omits the other employee from the lists. The question is, how do I fix it, and if this code has to be fixed my employee file? Here is my Employee code. Any tip/links/advice is helpful.

查看您的 equals 函数是否存在问题:如果名字和姓氏相同,您的 map 就会认为两个员工是相同的。您应该重写您的 equals 函数以将员工 ID 考虑在内。

关于java - Hashmap、排序/重复问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19636556/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com