- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Can we add null elements to a Set in Java?
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)
- Related Articles
- Can we insert null values in a Java list?
- Can we convert a list to a Set in Java?
- How can we add padding to a JTextField in Java?
- How can we add a JSONArray to JSONObject in Java?
- Add elements to a Set using Javascript
- How can we set a border to JCheckBox in Java?\n
- How can we set the margin to a JButton in Java?
- How can we set a background color to JSplitPane in Java?
- Can we convert a List to Set and back in Java?
- How can we convert list to Set in Java?
- How can we add/insert a JButton to JTable cell in Java?
- How can we add a JSONArray within JSONObject in Java?
- Add elements to a Vector in Java
- How can we set the background color to a JPanel in Java?
- How can we set the shortcut key to a JButton in Java?
