java java集合框架中为什么没有直接实现Bag?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15608667/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 20:15:16  来源:igfitidea点击:

Why is there no direct implemention of Bag in java collection framework?

javacollections

提问by Morteza Adi

I can't figure out why JCF (Java Collection Framework) does't have a Bagimplementation(to allow duplicates and not maintain order). Bagperformance would be much better than current Collection implementations in JCF.

我不明白为什么 JCF(Java 集合框架)没有Bag实现(允许重复而不是维护顺序)。 Bag性能会比 JCF 中当前的 Collection 实现要好得多。

  • I know how to implement Bagin Java.
  • I know Bagis available in Apache commons.
  • I know there are other implementations that can be used as a Bagbut there is so much work to do in other implementations compared to a Bag.
  • 我知道如何Bag在 Java 中实现。
  • 我知道Bag在 Apache 公共资源中可用。
  • 我知道还有其他实现可以用作 aBag但是与Bag.

Why has the Java Collections framework not provided direct implementations like this?

为什么 Java Collections 框架没有提供这样的直接实现?

采纳答案by Ajay George

Posting my comment as an answer since it answers this question best.

发布我的评论作为答案,因为它最好地回答了这个问题。

From the bug report filed here:

这里提交的错误报告:

There isn't a lot of enthusiasm among the maintainers of the Collection framework to design and implement these interfaces/classes. I personally can't recall having needed one. It would be more likely that a popular package developed outside the JDK would be imported into the JDK after having proved its worth in the real world.

Collection 框架的维护者对设计和实现这些接口/类的热情并不高。我个人不记得需要一个。在 JDK 之外开发的流行包更有可能在现实世界中证明其价值后被导入 JDK。

The need for having support for Bags is valid today.

对 Bags 支持的需求在今天是有效的。

Guavahas support for it. Also GS-Collections.

Guava支持它。还有GS-Collections

回答by Rahul

Currently, bag violates the collections contract. Many methods are in conflict with the current collections rules.

目前,bag 违反了收款合同。许多方法与当前的集合规则相冲突。

"Bag is a Collection that counts the number of times an object appears in the collection. Suppose you have a Bag that contains {a, a, b, c}. Calling getCount(Object)on awould return 2, while calling uniqueSet()would return {a, b, c}.

“Bag 是一个集合,它计算对象在集合中出现的次数。假设您有一个包含 的 Bag {a, a, b, c}。调用getCount(Object)ona将返回 2,而调用uniqueSet()将返回{a, b, c}

Note that this interface violates the Collection contract. The behavior specified in many of these methods is not the same as the behavior specified by Collection. The noncompliant methods are clearly marked with "(Violation)" in their summary line. A future version of this class will specify the same behavior as Collection, which unfortunately will break backwards compatibility with this version."

请注意,此接口违反了 Collection 合同。许多这些方法中指定的行为与 Collection 指定的行为不同。不合规的方法在其摘要行中清楚地标有“(违规)”。此类的未来版本将指定与 Collection 相同的行为,不幸的是,这将破坏与此版本的向后兼容性。”

 boolean add(java.lang.Object o)
      (Violation) Add the given object to the bag and keep a count.

 boolean removeAll(java.util.Collection c)
      (Violation) Remove all elements represented in the given collection, respecting cardinality.

Please see the link for more information: HERE

请查看链接以获取更多信息:这里

回答by Adisesha

JDK tries to give you implementation of common data structures and allow you to implement anything if common structures won't server your purpose. They may have thought that it is not common data structure.From practicality, it is not possible for them to implement every data structure out there or satisfy everybody's requirements. What you think common may not be common for majority.

JDK 尝试为您提供通用数据结构的实现,并允许您在通用结构不能满足您的目的时实现任何东西。他们可能认为这不是通用的数据结构。从实用性来看,他们不可能实现所有的数据结构或满足每个人的要求。您认为常见的对于大多数人来说可能并不常见。