class SomeRequest{
Set<Long> longs;
Optional<String> name;
Optional<Set<Long>> opLongs;
Optional<Long> toBeIgnored;
Collection<Field> fields
}
hashCode
。
private int getParameterHash(SomeRequest req){
SomeRequest newReq = new SomeRequest(
req.longs,
req.name,
req.opLongs,
null, //because I want to ignore this for my use case
req.fields
);
return newReq.hashCode();
}
有更好的方法吗?
Set
Collection
HashMap
会。那么我该如何忽略订单呢?
填写“ this”的意思是“计算自定义哈希代码”,如果有更好的方法是有争议的。
但是有一种替代方法。 如果不将自定义标签用于
HashSet
或
Set
或等效的键,则可以简单地将其定义为新方法。
Collection
不在乎订单,但Collection
取决于实际
fields.hashCode()
fields
类是什么。 集合的哈希码可能对订单敏感也可能不敏感。那么我该如何忽略订单?
确定您的情况是敏感的,您可以用一些代码替换呼叫,以迭代the的元素并计算其hashcodes的XOR。