[英]How to write equals and hashCode methods in java for attribute that is a list of other attributes
我有一個HashMap
,其中的鍵是一個類,值是一個整數。 我需要檢查地圖中是否已存在該類的對象。 我使用containsKey()
,但是由於某些原因,當我在equals()
和hashCode()
包含屬性sideDish
時,它不起作用。 這是我的課程代碼:
OrderItem類:
@ToString
@Entity
@Table(name="OrderItem")
public class OrderItem implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
@Getter @Setter
private Long id;
@ManyToOne
@Getter @Setter
private Food food;
@ManyToMany
@Getter @Setter
private List<SideDish> sideDishes;
public OrderItem() {}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((food == null) ? 0 : food.hashCode());
result = prime * result + ((sideDishes == null) ? 0 : sideDishes.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
OrderItem other = (OrderItem) obj;
if (food == null) {
if (other.food != null)
return false;
} else if (!food.equals(other.food))
return false;
if (sideDishes == null) {
if (other.sideDishes != null)
return false;
} else if (!sideDishes.equals(other.sideDishes))
return false;
return true;
}
}
食品類:
@ToString
@Entity
@Table(name="Food")
public class Food implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
@Getter @Setter
private Long id;
@Column(nullable = false, unique = true)
@NotNull(message = "Name cannot be null.")
@Getter @Setter
private String name;
@ManyToMany
@Getter @Setter
private List<SideDish> sidedishes;
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((foodtype == null) ? 0 : foodtype.hashCode());
result = prime * result + ((id == null) ? 0 : id.hashCode());
result = prime * result + ((name == null) ? 0 : name.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Food other = (Food) obj;
if (foodtype == null) {
if (other.foodtype != null)
return false;
} else if (!foodtype.equals(other.foodtype))
return false;
if (id == null) {
if (other.id != null)
return false;
} else if (!id.equals(other.id))
return false;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
return true;
}
}
SideDish類:
@Entity
@ToString(exclude= {"id","dishtype"})
@Table(name="SideDish")
public class SideDish implements Serializable, Comparable<SideDish>{
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Getter @Setter
private Long id;
@Getter @Setter
@Column(nullable = false, unique = true)
private String name;
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((id == null) ? 0 : id.hashCode());
result = prime * result + ((name == null) ? 0 : name.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
SideDish other = (SideDish) obj;
if (id == null) {
if (other.id != null)
return false;
} else if (!id.equals(other.id))
return false;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
return true;
}
}
出於某種原因,如果我從OrderItem
類的equals()
和hashCode()
中sideDish
屬性,則它可以正常工作。
但是我還需要將sideDish
作為對象標識的一部分進行檢查。
這是我的用法:
HashMap<OrderItem, Integer> orderItemsToSend = new HashMap<OrderItem, Integer>();
for (Order order : orders) {
for (OrderItem orderItem : order.getOrderItems()) {
int numSimilarOrders = getNumOfSimilarOrders(orderItem, orders);
if(!orderItemsToSend.containsKey(orderItem)) {
orderItemsToSend.put(orderItem, numSimilarOrders);
}else {
System.out.println("Vec je dodat item koji isti kao: " + orderItem.getFood().getName());
}
}
}
在OrderItem
類中, hashCode()
和equals()
取決於屬性List<SideDish> sideDishes
。
因此,如果sideDishes
更改,則hashCode()
sideDishes
更改(相等性sideDishes
更改)。
HashMap
使用hashCode()
和equals()
來存儲和查找作為key的對象。 它使用了一個稱為“ 哈希桶 ”的概念。 如果將密鑰放入HashMap
,然后hashCode()
更改,則該對象將位於錯誤的哈希存儲桶中,並且您將無法再次找到它。
密鑰是用於查找目的的東西,這就是“密鑰”一詞的含義。 密鑰的重要質量,無論是在數據庫中還是在哈希圖中,都是不可變性的。 因此,在Java中,這意味着更改其hashCode()
的對象將產生錯誤的密鑰。
這有點像文件系統通過文件名的哈希進行查找,但是隨后您更改了文件名,但沒有更新哈希。 您只能通過使用舊名稱進行查找來找到文件。
這個簡單的測試程序將說明這一點。
我們將2個對象存儲在HashMap
,然后更改hashCode()
。 映射仍然包含兩個對象,但是現在找不到其中之一或將其用於查找。
解決方案是使用一些簡單的不可變對象作為鍵 ,例如其數據庫ID的Long
。
示例輸出在代碼下方。
public class HashTest {
static class Hashable {
String name;
@Override
public int hashCode() {
return ((name == null) ? 0 : name.hashCode());
}
@Override
public boolean equals(Object object) {
return (object instanceof Hashable) && equals((Hashable) object);
}
private boolean equals(Hashable that) {
return Objects.equals(this.name, that.name);
}
@Override
public String toString() {
// Use identityHashCode() so we can really see which object is which
return "[" + name + ":" + System.identityHashCode(this) + "]";
}
}
public static void main(String[] args) {
Hashable one = new Hashable();
one.name = "one";
Hashable two = new Hashable();
two.name = "one";
print(one, two);
two.name = "two";
print(one, two);
HashMap<Hashable, Integer> map = new HashMap<>();
map.put(one, 1);
map.put(two, 2);
find(map, one, two);
one.name = "two"; // Let's confuse things
print(one, two);
find(map, one, two);
}
private static void print(Hashable one, Hashable two) {
System.out.print("Names:" + one.name + ":" + two.name);
System.out.print("\tHashcodes:" + one.hashCode() + ":" + two.hashCode());
System.out.println("\tEquals:" + one.equals(two));
}
private static void find(HashMap<Hashable, Integer> map, Hashable one, Hashable two) {
System.out.print(map);
System.out.print("\tFound: " + map.get(one));
System.out.println("\tFound: " + map.get(two));
}
}
樣本輸出:
Names:one:one Hashcodes:110182:110182 Equals:true
Names:one:two Hashcodes:110182:115276 Equals:false
{[one:366712642]=1, [two:1829164700]=2} Found: 1 Found: 2
Names:two:two Hashcodes:115276:115276 Equals:true
{[two:366712642]=1, [two:1829164700]=2} Found: 2 Found: 2
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.