HashSet中使用@Data注解问题
平时习惯使用lombok工具,免去了我们写get
、set
方法之类的,当然了,我们使用@Data注解后,equals()
、hashCode()
、toString()
也省却了。但是当你代码存在继承关系时,就得留心结果是否是你想要的了?
下面我直接列举个例子吧:
父类:
@Data
@NoArgsConstructor
@AllArgsConstructor
public class Tag {
private Long id;
private String tagName;
private String position;
}
子类:
@Data
@NoArgsConstructor
public class UserTag extends Tag {
private Long userId;
public UserTag(Long id, String tagName, String position, Long userId) {
super(id, tagName, position);
this.userId = userId;
}
}
其实关系就这么easy,最后我们test来说明问题
public class UserTagTest {
public static void main(String[] args) {
UserTag firstUserTag = new UserTag(1L, "tag1", "up", 2000L);
UserTag secondUserTag = new UserTag(2L, "tag2", "down", 2000L);
Set<Tag> tagSet = <span class="hljs-keyword">new</span> HashSet<>();
<span class="hljs-keyword">boolean</span> firstAdd = tagSet.add(firstUserTag);
<span class="hljs-keyword">boolean</span> secondAdd = tagSet.add(secondUserTag);
System.out.println(<span class="hljs-string">"firstAdd:"</span> + firstAdd);
System.out.println(<span class="hljs-string">"secondAdd:"</span> + secondAdd);
System.out.println(<span class="hljs-string">"tagSet size:"</span> + tagSet.size());
}
}
运行实际结果:
firstAdd:true
secondAdd:false
tagSet size:1
当看着实际结果和预期结果不同,当然了,很容易就想到是equals()
和hashCode()
的问题。最后我反编译看着@Data帮我们生成的equals()
和hashCode()
,如下:
public boolean equals(Object o) {
if (o == this) {
return true;
} else if (!(o instanceof UserTag)) {
return false;
} else {
UserTag other = (UserTag)o;
if (!other.canEqual(this)) {
return false;
} else {
Object this$userId = this.getUserId();
Object other$userId = other.getUserId();
if (this$userId == null) {
if (other$userId != null) {
return false;
}
} else if (!this$userId.equals(other$userId)) {
return false;
}
<span class="hljs-keyword">return</span> <span class="hljs-keyword">true</span>;
}
}
}
protected boolean canEqual(Object other) {
return other instanceof UserTag;
}
public int hashCode() {
int PRIME = true;
int result = 1;
Object $userId = this.getUserId();
int result = result * 59 + ($userId == null ? 43 : $userId.hashCode());
return result;
}
实际上只比较了userId
,说到这,得到上面的运行结果也就很正常了。
</div>