hashmap Java Map即使在重写hashcode和equals之后也无法工作
我有一个这样定义的自定义键对象
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
@Builder
@ToString
@JsonInclude(JsonInclude.Include.NON_NULL)
public class Key implements Serializable {
private String key; //JSON
private LocalDateTime expiry;
private long cacheId;
private LocalDateTime accessed;
public Key(Long cacheId, String key, LocalDateTime expiry) {
this.key = key;
this.expiry = expiry;
this.cacheId = cacheId;
this.accessed = LocalDateTime.now();
}
}
键中的键属性通常包含JSON字符串。 hashcode和equals被覆盖为
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Key key1 = (Key) o;
return cacheId == key1.cacheId && key.equals(key1.key);
}
@Override
public int hashCode() {
return Objects.hash(cacheId, key);
}
在另一个地方,我有一个树映射,定义为私有映射<;键,对象>;地图强>
当我试着呼叫map时。get()使用现有键(具有匹配的cacheId和字符串键值的键),我无法获取所需的值,即使在调试问题时,我可以看到映射包含具有匹配的cacheId和字符串键值的键的值
我做错什么了吗
共 (0) 个答案