本文介绍了是否有一个基本的Java集合实现,不允许使用空?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为Java 的状态:

例如,某些实现禁止null元素,有的对他们的类型元素的限制

我要寻找不需要订购了一套基本实现(如提供了接口),并且不不允许空。 ,的和的都允许null元素。此外,TreeSet中有元素实现的要求。

I am looking for a basic Set implementation that does not require ordering (as ArrayList provides for the List interface) and that does not permit null. TreeSet, HashSet, and LinkedHashSet all allow null elements. Additionally, TreeSet has the requirement that elements implement Comparable.

似乎没有这样的基本设置目前存在。有谁知道为什么吗?或者,如果确实存在在那里我可以找到它?

It seems that no such basic Set exists currently. Does anyone know why? Or if one does exist where I can find it?

:我不希望允许空值,因为后面的code我的班会遍历集合中的所有元素,并调用特定的方法。 (我实际使用的HashSet < MyRandomObject >)。我宁愿快速失败不是失败后或意外招致了一些奇怪的行为,由于集中的空之中。

: I do not want to allow nulls, because later in the code my class will iterate over all elements in the collection and call a specific method. (I'm actually using HashSet<MyRandomObject>). I would rather fail fast than fail later or accidentally incur some bizarre behavior due to a null being in the set.

推荐答案

不是扩展特定的实现更好的,你可以随便写设置的代理执行的检查秒。这类似于 Col​​lections.checkedSet 。除了是适用于任何实现,也可以确保您已覆盖所有适用的方法。许多漏洞已经被延伸的话已经在以后的版本中添加的其他方法的具体集合发现。

Better than extending a particular implementation, you can easily write a proxy implementation of Set that checks for nulls. This analogous to Collections.checkedSet. Other than being applicable to any implementation, you can also be sure that you have overridden all applicable methods. Many flaws have been found by extending concrete collections which then have additional methods added in later versions.

这篇关于是否有一个基本的Java集合实现,不允许使用空?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 15:36