[英]How can I use java decimalformat to change my double type number from x.0E-4 to 0.000x?
[英]How can I remove an object of type X from a hashtable given one unique attribute of X in Java?
假設我有一個上課的人
public class Person {
int personID = 123;
String fname = John;
String lname = Doe;
}
並且我創建了一個哈希表。 (將人同時用作鍵和值)。 現在,假設每個人都有一個唯一的ID,並假設哈希表充滿了人,我想給定一個唯一的ID(int),從與該ID對應的哈希表中刪除該人。
問題在於用什么參數賦予hashtable.remove()函數,因為它采用了Person類型而不是int類型的對象。
注意:假設鍵和值均為Person類型,這是設計要求,不能更改
編輯:我已經覆蓋了人員類的hashcode()和equals()方法,它們現在都依賴於personID。 但是我仍然不知道要傳遞什么hashtable.remove()方法,因為它采用的是Person類型,並且我想根據用戶提供的int(即personID)刪除它
如果您已基於personID覆蓋了equals()和hashCode()方法,則只需使用要搜索的personID創建一個Person對象,並相應地搜索/刪除此person。
假設您要搜索123 personID。
您可以Person p = new Person(); p.personID = 123; p.fName = "XYZ"; p.lName = "ABC"; myHashtable.remove(p);
Person p = new Person(); p.personID = 123; p.fName = "XYZ"; p.lName = "ABC"; myHashtable.remove(p);
由於equals()和hashCode()基於personID。 因此表格將根據personID進行搜索/刪除
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.