Java LinkedHashSet未删除
我正在经历一种奇怪的行为,我不知道为什么。我试图从Java LinkedHashSet未删除,java,data-structures,collections,Java,Data Structures,Collections,我正在经历一种奇怪的行为,我不知道为什么。我试图从LinkedHashSet中删除一个元素,但它没有被删除 我的类文档定义了: protected Set<Author> authors = new LinkedHashSet<>(); 然后,通过一些调试打印删除的代码: public void removeAuthor(Author author) { System.out.println(">removeAuthor [" + author.hashC
LinkedHashSet
中删除一个元素,但它没有被删除
我的类文档定义了:
protected Set<Author> authors = new LinkedHashSet<>();
然后,通过一些调试打印删除的代码:
public void removeAuthor(Author author) {
System.out.println(">removeAuthor [" + author.hashCode() + "]: " + author);
if (document.getAuthors() != null) {
System.out.println("[" + document.getAuthors().size() + "] Authors BEFORE:");
document.getAuthors().forEach((pp) -> {
System.out.println("[" + pp.hashCode() + "] " + pp);
});
}
if (document != null) {
if (document.getAuthors() != null) {
document.getAuthors().remove(author);
}
}
if (document.getAuthors() != null) {
System.out.println("[" + document.getAuthors().size() + "] Authors AFTER:");
document.getAuthors().forEach((pp) -> {
System.out.println("[" + pp.hashCode() + "] " + pp);
});
}
}
这将打印以下内容(我添加我的评论):
我通常习惯于使用集合。这可能是个愚蠢的错误,但我找不到它在哪里。此外,compareTo或其他方法似乎不用于测试相等性。由于调试,我想我已经解决了这个问题。这是Ajax使用中的一个明显错误。当创建
LinkedHashSet
时,元素将根据其hashCode
添加到索引中的哈希表中。有时,Ajax JSF操作可以更改内存中对象的角色
,这使它们更改哈希。但是,哈希表中它们最初插入的位置根本没有改变!因此,您现在有了一个元素,该元素具有要删除的特定散列,该元素确实存在于您的集合中(如图所示,散列是相同的),但当实际代码尝试从表[(table.length-1)&散列]
中删除该元素时,却从未找到它,因为它不在那个位置-它最初是用另一个散列值添加的。这就解决了这个问题,而且完全符合逻辑。你能构造一个吗?当你调用contains方法时你所拥有的document.getAuthors().contains(author)
?author.role是什么类型的?如果它是一个自定义类,那么它也实现了equals?Author角色是一个字符串,与id相同。但是equals似乎没有被调用System.out.println(“contains”+document.getAuthors().contains(Author))
返回包含false
事实上,它没有那么严重。只是有些ajax方法允许更改角色(以及其他属性),这是完全合理的,它们恰好是关键的一部分。所以当这种变化发生时,集合元素应该被删除并再次插入。现在处于控制之下。谢谢!=)
public void removeAuthor(Author author) {
System.out.println(">removeAuthor [" + author.hashCode() + "]: " + author);
if (document.getAuthors() != null) {
System.out.println("[" + document.getAuthors().size() + "] Authors BEFORE:");
document.getAuthors().forEach((pp) -> {
System.out.println("[" + pp.hashCode() + "] " + pp);
});
}
if (document != null) {
if (document.getAuthors() != null) {
document.getAuthors().remove(author);
}
}
if (document.getAuthors() != null) {
System.out.println("[" + document.getAuthors().size() + "] Authors AFTER:");
document.getAuthors().forEach((pp) -> {
System.out.println("[" + pp.hashCode() + "] " + pp);
});
}
}
// Call to hashCode() to print the author that will be removed
Calling hashcode: 400605768
// Author that will be removed
>removeAuthor [400605768]: Author{id=self, name=Self Centre, forename=null, familyName=null, role=Writer}
// List before calling remove, it gives 2 authors
[2] Authors BEFORE:
Calling hashcode: -1820871746
[-1820871746] Author{id=self, name=Self Centre, forename=null, familyName=null, role=Researcher}
Calling hashcode: 400605768
[400605768] Author{id=self, name=Self Centre, forename=null, familyName=null, role=Writer}
// This hashCode call is probably done by remove(). As it can be seen, the object to be removed *is* on the Set
Calling hashcode: 400605768
// List after calling remove, it gives again 2 authors
[2] Authors AFTER:
Calling hashcode: -1820871746
[-1820871746] Author{id=self, name=Self Centre, forename=null, familyName=null, role=Researcher}
Calling hashcode: 400605768
[400605768] Author{id=self, name=Self Centre, forename=null, familyName=null, role=Writer}
Calling hashcode: -1820871746