Home >Java >javaTutorial >Keep \'Vacuous truth\' on top of mind while coding with java.util.stream API

Keep \'Vacuous truth\' on top of mind while coding with java.util.stream API

王林
王林Original
2024-09-03 15:52:21720browse

Keep

  • Introduction
  • What is Vacuous Truth?
    • Problem Statement
    • Enter Vacuous Truth
      • Mathematical Definition of Vacuous Truth
    • Why Does This Matter in Programming?
    • References

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 ase 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:
    • boolean allMatch(Predicate predicate)
    • boolean noneMatch(Predicate predicate)

The above is the detailed content of Keep \'Vacuous truth\' on top of mind while coding with java.util.stream API. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn