所以我有这个代码:
List<MyDataType> test = map.get(id)
MyDataType test1 = new MyDataType("XXX", 1);
System.out.println(test.contains(test1)); // returns false
System.out.println(test.get(0).equals(test1)); // returns true
System.out.println(test.remove(test1)); // returns false
System.out.println(test); // still the original list
我已经重写了MyDataType中的.equals
and.hashCode()
方法,而且是一致的。此外,map
is 类型ListMultimap<Long, DestQuanTuple>
(忘记在我的 OP 中提及这一点。)但是,调用:
map.remove(sourceContainerId, test1)
也不会修改test
。
MyDataType
班级:
public class MyDataType implements Comparable<MyDataType> {
private String destination;
private int quantity;
// Constructor initializes destination and quantity
// Getters exist to get destination and quantity
public boolean equals (MyDataType other) {
boolean destinationSame = false, quantitySame = false;
destinationsame = this.getDestination().equals(other.getDestination());
Integer thisQuantity = (Integer) this.getQuantity();
Integer otherQuantity = (Integer) other.getQuantity();
quantitySame = thisQuantity.equals(otherQuantity);
return destinationSame && quantitySame;
}
public int hashCode() {
return destination.hashCode() + quantity;
}
public int compareTo(MyDataType other) {
Integer myQuantity = this.getQuantity();
if (myQuantity.compareTo(other.getQuantity()) != 0) {
return myQuantity.compareTo(otherTuple.getQuantity());
}
else {
return this.getDestination().compareTo(otherTuple.getDestination());
}
}
}
知道为什么我没有得到我想要的功能吗?