Test with Null Elements
In the previous steps, we've seen how HashSet
handles duplicate non-null elements. Now, let's explore how HashSet
behaves when we try to add null
elements. Understanding how collections handle null
is important because it can sometimes lead to unexpected behavior or errors if not handled carefully.
A HashSet
allows one null
element. If you try to add null
multiple times, only the first null
will be stored.
Let's modify our DuplicateCheck.java
program one more time to test this.
Open the DuplicateCheck.java
file in the WebIDE Code Editor.
Modify the main
method to include null
values in the list:
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
public class DuplicateCheck {
public static void main(String[] args) {
// Create a list with some duplicate and null elements
List<String> names = new ArrayList<>();
names.add("Alice");
names.add("Bob");
names.add(null); // Add null
names.add("Alice"); // Duplicate
names.add("Charlie");
names.add(null); // Add null again
names.add("Bob"); // Duplicate
names.add("David");
names.add(null); // Add null a third time
System.out.println("Original List: " + names);
System.out.println("Size of Original List: " + names.size());
// Create a HashSet from the list
Set<String> uniqueNamesSet = new HashSet<>(names);
System.out.println("Set created from List: " + uniqueNamesSet);
System.out.println("Size of Set: " + uniqueNamesSet.size());
// The difference in size tells us how many duplicates were removed
// Note: This calculation is less straightforward with nulls and duplicates combined
// int duplicatesCount = names.size() - uniqueNamesSet.size();
// System.out.println("Number of duplicates (excluding first occurrence): " + duplicatesCount);
}
}
Here, we've added null
to the names
list multiple times. We've also commented out the duplicate count calculation because it becomes less meaningful when null
is involved and we are focusing on the set's behavior with null
.
Save the modified DuplicateCheck.java
file.
Compile the program in the Terminal:
javac DuplicateCheck.java
If the compilation is successful, run the program:
java DuplicateCheck
You should see output similar to this:
Original List: [Alice, Bob, null, Alice, Charlie, null, Bob, David, null]
Size of Original List: 9
Set created from List: [null, Alice, Bob, Charlie, David]
Size of Set: 5
Observe the output:
- The
Original List
shows all the elements, including the multiple null
values. Its size is 9.
- The
Set created from List
contains only unique elements. Notice that null
appears only once in the set, even though it was added multiple times to the list. The size of the set is 5 (Alice, Bob, Charlie, David, and null).
This confirms that HashSet
allows one null
element and treats subsequent null
additions as duplicates, just like any other element.
You have now successfully tested how HashSet
handles null
elements. This concludes our exploration of using HashSet
for duplicate checking and understanding its behavior with unique and null elements.