Heim > Java > javaLernprogramm > Behalten Sie beim Codieren mit der java.util.stream-API die „leere Wahrheit' im Hinterkopf

Behalten Sie beim Codieren mit der java.util.stream-API die „leere Wahrheit' im Hinterkopf

王林
Freigeben: 2024-09-03 15:52:21
Original
740 Leute haben es durchsucht

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);
  }
Nach dem Login kopieren

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);
  }
Nach dem Login kopieren

References

  • Wikipedia: Vacuous truth
  • API note in Java documentation:
    • boolean allMatch(Predicate predicate)
    • boolean noneMatch(Predicate predicate)

Das obige ist der detaillierte Inhalt vonBehalten Sie beim Codieren mit der java.util.stream-API die „leere Wahrheit' im Hinterkopf. Für weitere Informationen folgen Sie bitte anderen verwandten Artikeln auf der PHP chinesischen Website!

Quelle:dev.to
Erklärung dieser Website
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn
Beliebte Tutorials
Mehr>
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage