Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to workaround this JDK limitation?

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

Do you know one?

like image 536
Stephan Avatar asked Aug 09 '11 13:08

Stephan


2 Answers

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.

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

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

like image 90
maaartinus Avatar answered Oct 02 '22 22:10

maaartinus


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

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

like image 33
Joachim Sauer Avatar answered Oct 02 '22 21:10

Joachim Sauer