"If a worker wants to do his job well, he must first sharpen his tools." - Confucius, "The Analects of Confucius. Lu Linggong"
Front page > Programming > 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

Published on 2024-11-08
Browse:269

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 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 numbers) {
    return !numbers.isEmpty() && numbers.stream().allMatch(integer -> integer % 2 != 0);
  }

References

  • Wikipedia: Vacuous truth
  • API note in Java documentation:
    • boolean allMatch(Predicate super T> predicate)
    • boolean noneMatch(Predicate super T> predicate)
Release Statement This article is reproduced at: https://dev.to/prabhatkjena/keep-vacuous-truth-on-top-of-mind-while-coding-with-javautilstream-api-4lnb?1 If there is any infringement, please contact study_golang@163 .comdelete
Latest tutorial More>

Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.

Copyright© 2022 湘ICP备2022001581号-3