Is there a basic Java Set implementation that does not permit nulls?

33,911

Solution 1

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.

Solution 2

I would say use composition instead of inheritance... it might be more work but it'll be more stable in the face of any changes that Sun might make to the Collections Framework.

public class NoNullSet<E> implements Set<E>
{
   /** The set that is wrapped. */
   final private Set<E> wrappedSet = new HashSet<E>();

   public boolean add(E e)
   {
     if (e == null) 
       throw new IllegalArgumentException("You cannot add null to a NoNullSet");
     return wrappedSet.add(e);
   }

   public boolean addAll(Collection<? extends E> c)
   {
     for (E e : c) add(e);
   }

   public void clear()
   { wrappedSet.clear(); }

   public boolean contains(Object o)
   { return wrappedSet.contains(o); }

   ... wrap the rest of them ...
}

Note that this implementation does not depend on addAll calling add (which is an implementation detail and should not be used because it cannot be guaranteed to remain true in all Java releases).

Solution 3

There is no basic proprietary Set implementation that ignores or constrains null! There is EnumSet, but that one is tailors for the containment of enum types.

However, creating your own implementation can be avoided, if you use either Guava or Commons Collections:

1. Guava Solution:

Set noNulls = Constraints.constrainedSet(new HashSet(), Constraints.notNull());

2. Commons Collections:

Set noNulls = new HashSet();
CollectionUtils.addIgnoreNull(noNulls, object);

Solution 4

You could use apache collections and its PredicatedCollection class, and set the predicate to not allow nulls. You will get exceptions if someone sends nulls in.

Solution 5

Yes -- in the docs for com.google.common.collect.ImmutableSet:

A high-performance, immutable Set with reliable, user-specified iteration order. Does not permit null elements.

Share:
33,911
Aaron K
Author by

Aaron K

Currently a Ruby and RoR dev working in the DC area. Perpetual student, code tinkerer, and helplessly addicted to good coffee.

Updated on February 28, 2020

Comments

  • Aaron K
    Aaron K about 4 years

    The API for the Java Set interface states:

    For example, some implementations prohibit null elements and some have restrictions on the types of their elements

    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?

    [Edit]: 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.