如何解决这个JDK限制?

时间:2021-09-17 19:43:27

I'm looking for a class from the Java Collection Framework that would not allow null elements.

我正在寻找Java Collection Framework中不允许使用null元素的类。

Do you know one?

你知道吗?

7 个解决方案

#1


20  

Most Queue implementations (with the notable exception of LinkedList) don't accept null.

大多数Queue实现(使用LinkedList的明显例外)不接受null。

EnumSet is a special-purpose Set implementation that doesn't allow null values.

EnumSet是一个特殊用途的Set实现,它不允许空值。

#2


47  

Use Constraints:

使用约束:

import com.google.common.collect.Constraints;
...
Constraints.constrainedList(new ArrayList(), Constraints.notNull())

from Guava for maximum flexibility.

从番石榴获得最大的灵活性。

UPDATE: Guava Constraints has been deprecated in Release 15 - apparently without replacement.

更新:Guava约束已在第15版中弃用 - 显然没有替代。

UPDATE 2: As of now (Guava 19.0-rc2) Constrains is still there and not deprecated anymore. However, it's missing from the Javadoc.

更新2:截至目前(Guava 19.0-rc2)Constrains仍在那里,不再被弃用了。然而,它在Javadoc中缺失了。

I'm afraid that the Javadoc is right as MapConstraint have been deprecated in Release 19, too

我担心Javadoc是正确的,因为MapConstraint也在第19版中被弃用了

#3


12  

There's a roundup of such collections here.

这里收集了这样的集合。

#4


12  

Apache Commons Framework - CollectionUtils.addIgnoreNull

Apache Commons Framework - CollectionUtils.addIgnoreNull

Adds to myList if myObj is not null.

如果myObj不为null,则添加到myList。

org.apache.commons.collections.CollectionUtils.addIgnoreNull(myList, myObj)

org.apache.commons.collections.CollectionUtils.addIgnoreNull(myList,myObj)

#5


4  

Using Google Guava Predicates (the answer from @Joachim Sauer is deprecated)

使用Google Guava Predicates(@Joachim Sauer的答案已被弃用)

//list is the variable where we want to remove null elements
List nullRemovedList=Lists.newArrayList(Iterables.filter(list, Predicates.notNull()));

//Or convert to Immutable list
List nullRemovedList2=ImmutableList.copyOf(Iterables.filter(list, Predicates.notNull()));

#6


1  

Hashtable does not allow null keys or values.

Hashtable不允许使用null键或值。

#7


1  

Start here, the Collections API Page. Check out the "See Also" section. Follow the links. The decision to allow or disallow null is made by the implementing class; just follow the links in the various "See Also" sections to get to the implementing classes (for example, HashMap) then look at the insertion methods (generally a variation on add, push, or put) to see if that implementation permits null.

从这里开始,收集API页面。查看“另请参阅”部分。点击链接。允许或禁止null的决定由实施类决定;只需按照各种“另请参见”部分中的链接访问实现类(例如,HashMap),然后查看插入方法(通常是添加,推送或放置的变体),以查看该实现是否允许null。

#1


20  

Most Queue implementations (with the notable exception of LinkedList) don't accept null.

大多数Queue实现(使用LinkedList的明显例外)不接受null。

EnumSet is a special-purpose Set implementation that doesn't allow null values.

EnumSet是一个特殊用途的Set实现,它不允许空值。

#2


47  

Use Constraints:

使用约束:

import com.google.common.collect.Constraints;
...
Constraints.constrainedList(new ArrayList(), Constraints.notNull())

from Guava for maximum flexibility.

从番石榴获得最大的灵活性。

UPDATE: Guava Constraints has been deprecated in Release 15 - apparently without replacement.

更新:Guava约束已在第15版中弃用 - 显然没有替代。

UPDATE 2: As of now (Guava 19.0-rc2) Constrains is still there and not deprecated anymore. However, it's missing from the Javadoc.

更新2:截至目前(Guava 19.0-rc2)Constrains仍在那里,不再被弃用了。然而,它在Javadoc中缺失了。

I'm afraid that the Javadoc is right as MapConstraint have been deprecated in Release 19, too

我担心Javadoc是正确的,因为MapConstraint也在第19版中被弃用了

#3


12  

There's a roundup of such collections here.

这里收集了这样的集合。

#4


12  

Apache Commons Framework - CollectionUtils.addIgnoreNull

Apache Commons Framework - CollectionUtils.addIgnoreNull

Adds to myList if myObj is not null.

如果myObj不为null,则添加到myList。

org.apache.commons.collections.CollectionUtils.addIgnoreNull(myList, myObj)

org.apache.commons.collections.CollectionUtils.addIgnoreNull(myList,myObj)

#5


4  

Using Google Guava Predicates (the answer from @Joachim Sauer is deprecated)

使用Google Guava Predicates(@Joachim Sauer的答案已被弃用)

//list is the variable where we want to remove null elements
List nullRemovedList=Lists.newArrayList(Iterables.filter(list, Predicates.notNull()));

//Or convert to Immutable list
List nullRemovedList2=ImmutableList.copyOf(Iterables.filter(list, Predicates.notNull()));

#6


1  

Hashtable does not allow null keys or values.

Hashtable不允许使用null键或值。

#7


1  

Start here, the Collections API Page. Check out the "See Also" section. Follow the links. The decision to allow or disallow null is made by the implementing class; just follow the links in the various "See Also" sections to get to the implementing classes (for example, HashMap) then look at the insertion methods (generally a variation on add, push, or put) to see if that implementation permits null.

从这里开始,收集API页面。查看“另请参阅”部分。点击链接。允许或禁止null的决定由实施类决定;只需按照各种“另请参见”部分中的链接访问实现类(例如,HashMap),然后查看插入方法(通常是添加,推送或放置的变体),以查看该实现是否允许null。