Keep "Vacuous truth" on top of mind while coding with java.util.stream API
Prabhat Kumar
Posted on September 2, 2024
Introduction
Have you ever encountered a scenario in programming where you need to check if all elements in a list or stream satisfy a certain condition? This is a common pattern in coding, but what happens when the list is empty? This is where the concept of vacuous truth comes into play.
What is Vacuous Truth?
Before we dive into the mathematical definition, let’s start with a practical example in code to understand what vacuous truth is.
Problem Statement
Imagine you are tasked with checking if all elements in a list satisfy a certain condition. If they do, you perform a specific action. For example, consider the following Java code snippet:
public static void main(String[] args) {
// Example - 1, expected to do something
if (allNumbersAreOdd(Arrays.asList(1, 3, 5))) {
System.out.println("do something 1");
}
// Example - 2, NOT expected to do anything because NOT all numbers are odd
if (allNumbersAreOdd(Arrays.asList(1, 2, 3, 4, 5))) {
System.out.println("do something 2");
}
// Example - 3, NOT expected to do anything because list is empty so there is no odd number.
/* This is the surprising element which is known as "Vacuous truth" and it will print "do something".
* It is applicable to both allMatch(Predicate<? super T> predicate)
* and noneMatch(Predicate<? super T> predicate) */
if (allNumbersAreOdd(Collections.emptyList())) {
System.out.println("do something 3");
}
}
private static boolean allNumbersAreOdd(@Nonnull List<Integer> numbers) {
return numbers.stream().allMatch(integer -> integer % 2 != 0);
}
The third example is particularly interesting. Why does it return "All numbers are odd" when the list is empty?
Enter Vacuous Truth
This behavior is an example of vacuous truth. In mathematical logic, a statement that asserts something about all elements of an empty set is considered true. This is because there are no elements in the set to contradict the statement.
Mathematical Definition of Vacuous Truth
According to Wikipedia:
"A vacuous truth is a statement that asserts that all members of the empty set have a certain property. Such statements are considered true because there are no counterexamples in the empty set."
In other words, when we say, "All elements of set S have property P," and if S is empty, this statement is vacuously true because there isn’t a single element in S that could potentially violate property P
Why Does This Matter in Programming?
Understanding vacuous truth is important in programming because it can impact the logic and outcomes of your code, especially when dealing with collections, streams, or any scenario where your input could potentially be empty.
Conclusion
Next time you're writing a function that checks if all elements in a list or stream satisfy a condition, remember the concept of vacuous truth. It explains why your code might behave in an unexpected way when the input is empty. Being aware of this can help you write more robust and predictable programs.
If you have requirement of an empty list/stream must not be evaluated as true, then you have to consider additional check on list/stream.
private static boolean allNumbersAreOdd(@Nonnull List<Integer> numbers) {
return !numbers.isEmpty() && numbers.stream().allMatch(integer -> integer % 2 != 0);
}
References
- Wikipedia: Vacuous truth
- API note in Java documentation:
Posted on September 2, 2024
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.