Test with Null and Empty Lists
In real-world programming, it's important to consider edge cases, such as when a list might be empty or even null
. Our current containsDuplicates
method works well for lists with elements, but what happens if we pass an empty list or a null
list?
Let's test this by adding more examples to our main
method in ~/project/DuplicateDetector.java
. Open the file in the Code Editor and add the following lines to the main
method, after the existing code:
System.out.println("\nEmpty list: " + new ArrayList<>());
System.out.println("Contains duplicates? " + containsDuplicates(new ArrayList<>())); // Expected: false
List<String> nullList = null;
System.out.println("\nNull list: " + nullList);
// The following line will cause a NullPointerException if not handled
// System.out.println("Contains duplicates? " + containsDuplicates(nullList));
Save the file (Ctrl+S or Cmd+S).
Now, compile and run the program again.
Compile:
javac DuplicateDetector.java
Run:
java DuplicateDetector
You should see the output for the empty list:
List with duplicates: [apple, banana, apple, orange]
Contains duplicates? true
List without duplicates: [grape, mango, kiwi]
Contains duplicates? false
Empty list: []
Contains duplicates? false
The output for the empty list is correct; an empty list does not contain duplicates.
However, if you uncomment the line System.out.println("Contains duplicates? " + containsDuplicates(nullList));
and try to compile and run, you will get a NullPointerException
. This happens because we are trying to create a HashSet
from a null
list, which is not allowed.
To make our containsDuplicates
method more robust, we should handle the case where the input list is null
. We can add a check at the beginning of the method.
Modify the containsDuplicates
method in ~/project/DuplicateDetector.java
to include a null check:
public static boolean containsDuplicates(List<String> list) {
// Handle null input
if (list == null) {
return false; // A null list does not contain duplicates
}
// Create a HashSet from the list
Set<String> uniqueElements = new HashSet<>(list);
// Compare the size of the list with the size of the HashSet
return list.size() != uniqueElements.size();
}
Now, uncomment the line that tests the null list in the main
method:
List<String> nullList = null;
System.out.println("\nNull list: " + nullList);
System.out.println("Contains duplicates? " + containsDuplicates(nullList)); // Expected: false
Save the file (Ctrl+S or Cmd+S).
Compile and run the program one last time.
Compile:
javac DuplicateDetector.java
Run:
java DuplicateDetector
The output should now include the result for the null list without crashing:
List with duplicates: [apple, banana, apple, orange]
Contains duplicates? true
List without duplicates: [grape, mango, kiwi]
Contains duplicates? false
Empty list: []
Contains duplicates? false
Null list: null
Contains duplicates? false
By adding the null check, our containsDuplicates
method is now more robust and can handle null
input gracefully. This is an important practice in programming to prevent unexpected errors.