Can we add null elements to a Set in Java?

Java 8Object Oriented ProgrammingProgramming

A Set is a collection that cannot contain duplicate elements. It models the mathematical set abstraction.

It does not allow duplicate elements and allow one null value at most.

Set also adds a stronger contract on the behavior of the equals and hashCode operations, allowing Set instances to be compared meaningfully even if their implementation types differ.

There are three classes implementing this interface −

  • HashSet − Set implementation based on hash table.
  • LinkedHashSet − HashSet implementation based on linked list.
  • TreeSet − Set implementation based on trees.

Null values in a Set object

As per the definition a set object does not allow duplicate values but it does allow at most one null value.

Null values in HashSet − The HashSet object allows null values but, you can add only one null element to it. Though you add more null values if you try to print its contents, it displays only one null.

Example

import java.util.HashSet;
import java.util.Set;
public class HashSetExample {
   public static void main(String args[]) {
      Set<Integer> hashSet = new HashSet<Integer>();
      //Populating the HashSet
      hashSet.add(1124);
      hashSet.add(3654);
      hashSet.add(7854);
      hashSet.add(9945);
      System.out.println(hashSet);
      //Adding null elements
      hashSet.add(null);
      hashSet.add(null);
      hashSet.add(null);
      System.out.println(hashSet);
   }
}

Output

[1124, 3654, 9945, 7854]
[null, 1124, 3654, 9945, 7854]

Null values in LinkedHashSet: Just like the HashSet object, this also allows null values but, you can add only one null element to it. Though you add more null values if you try to print its contents, it displays only one null.

Example

import java.util.LinkedHashSet;
import java.util.Set;
public class LinkedHashSetExample {
   public static void main(String args[]) {
      Set<Integer> linkedHashSet = new LinkedHashSet<Integer>();
      //Populating the HashSet
      linkedHashSet.add(1124);
      linkedHashSet.add(3654);
      linkedHashSet.add(7854);
      linkedHashSet.add(9945);
      System.out.println(linkedHashSet);
      //Adding null elements
      linkedHashSet.add(null);
      linkedHashSet.add(null);
      linkedHashSet.add(null);
      System.out.println(linkedHashSet);
   }
}

Output

[1124, 3654, 9945, 7854]
[null, 1124, 3654, 9945, 7854]

Null values in TreeSet − The TreeSet object doesn’t allows null values but, If you try to add them, a runtime exception will be generated at.

Example

import java.util.Set;
import java.util.TreeSet;
public class TreeSetExample {
   public static void main(String args[]) {
      Set<Integer> treeSet = new TreeSet<Integer>();
      //Populating the HashSet
      treeSet.add(1124);
      treeSet.add(3654);
      treeSet.add(7854);
      treeSet.add(9945);
      System.out.println(treeSet);
      //Adding null elements
      treeSet.add(null);
      treeSet.add(null);
      treeSet.add(null);
      System.out.println(treeSet);
   }
}

Run time exception

[1124, 3654, 7854, 9945]
Exception in thread "main" java.lang.NullPointerException
   at java.util.TreeMap.put(Unknown Source)
   at java.util.TreeSet.add(Unknown Source)
   at MyPackage.TreeSetExample.main(TreeSetExample.java:16)
raja
Published on 07-Aug-2019 15:01:05
Advertisements