为什么当我向 TreeSet 推入三个对象时,它只能容纳一个对象?

发布于 2025-01-07 03:36:43 字数 999 浏览 1 评论 0原文

我想将一些对象推入 TreeSet(在 BigHeap 类中),但我遇到了问题:

这是我的主类:

# --------- Print 3 -----------
Logger.info("items.size() = " + items.size()); 
for (Item item : items) {
  Long score = item.getScoreByQueryItems(queryItems);
  Long itemId = item.id;
  ItemCacheNode node = new ItemCacheNode(itemId,score);
  bigHeap.push(node);  <----- here is the push action ---------
  # --------------Print three times------------
  Logger.info("node.itemId = " + node.getItemId()); 
}
# ---------------Print 1----------
Logger.info("bigHeap.getTreeSet().size() = " + bigHeap.getTreeSet().size()); 

这是我的 BigHeap.java:

public class BigHeap<T> {
 private TreeSet<T> treeSet;
 public BigHeap(Comparator<T> comparator){
   this.treeSet = new TreeSet<T>(comparator);
 }
 public void push(T o){
   treeSet.add(o);
 }
 public TreeSet<T> getTreeSet(){
   return this.treeSet;
 }
}

问题是,为什么 bigHeap 推入三次(不同的对象)但只保留一个毕竟是对象。

I want to push some objects into a TreeSet(inside BigHeap class) but i got problem :

here is my Main class:

# --------- Print 3 -----------
Logger.info("items.size() = " + items.size()); 
for (Item item : items) {
  Long score = item.getScoreByQueryItems(queryItems);
  Long itemId = item.id;
  ItemCacheNode node = new ItemCacheNode(itemId,score);
  bigHeap.push(node);  <----- here is the push action ---------
  # --------------Print three times------------
  Logger.info("node.itemId = " + node.getItemId()); 
}
# ---------------Print 1----------
Logger.info("bigHeap.getTreeSet().size() = " + bigHeap.getTreeSet().size()); 

And here's my BigHeap.java:

public class BigHeap<T> {
 private TreeSet<T> treeSet;
 public BigHeap(Comparator<T> comparator){
   this.treeSet = new TreeSet<T>(comparator);
 }
 public void push(T o){
   treeSet.add(o);
 }
 public TreeSet<T> getTreeSet(){
   return this.treeSet;
 }
}

The problem is , why the bigHeap push three times(different objects) but only hold one object after all.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

∞琼窗梦回ˉ 2025-01-14 03:36:43

这可能是由于 Set 不允许元素重复造成的。

您正在将 ItemCacheNode 类型的对象推送到名为 bigHeap 的 set ADT 中。现在,当将新对象添加到集合中时,按照

元素使用其自然顺序或按
在设定的创建时间提供比较器,具体取决于哪个
使用构造函数。

错误的比较器会覆盖 TreeSet 原生的听起来 equals 方法,当且仅当未使用 Set 接口正确实现 Treeset。根据上述文档,

请注意,集合维护的顺序(无论是否是显式的)
提供了比较器)必须与 equals 一致,如果是的话
正确实现Set接口。 (参见可比较或比较器
与 equals 一致的精确定义。) 就是这样
因为 Set 接口是根据 equals 操作定义的,
但是 TreeSet 实例使用其执行所有元素比较
CompareTo(或比较)方法,因此两个元素被视为相等
通过这种方法,从集合的角度来看,它们是相等的。这
即使集合的顺序不一致,集合的行为也是明确定义的
与平等;它只是没有遵守 Set 的一般契约
界面。

您的定义,

private TreeSet; treeSet = new TreeSet(comparator);

忽略了接口的使用,因此,通过不遵守 Set 的非重复功能的一般思想,显示了此错误的功能。代码>.

This would be something that could be attributed to the reason that Set doesn't allow for duplication of elements.

You are pushing objects of type ItemCacheNode into a set ADT named bigHeap. Now, when a new object is added to the set, as per the docs,

The elements are ordered using their natural ordering, or by a
Comparator provided at set creation time, depending on which
constructor is used.

An erroneous comparator would override the otherwise sounds equals method native to a TreeSet iff the Treeset is not correctly implemented using the Set interface. As per the aforementioned docs,

Note that the ordering maintained by a set (whether or not an explicit
comparator is provided) must be consistent with equals if it is to
correctly implement the Set interface. (See Comparable or Comparator
for a precise definition of consistent with equals.) This is so
because the Set interface is defined in terms of the equals operation,
but a TreeSet instance performs all element comparisons using its
compareTo (or compare) method, so two elements that are deemed equal
by this method are, from the standpoint of the set, equal. The
behavior of a set is well-defined even if its ordering is inconsistent
with equals; it just fails to obey the general contract of the Set
interface.

Your definition,

private TreeSet<T> treeSet = new TreeSet<T>(comparator);

ignores utilization of the interface and as a result, showed this erroneous functionality, by not complying with the general idea of non-duplication functionality of a Set.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文