DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Alternative Structured Concurrency
  • Designing Effective Meetings in Tech: From Time Wasters to Strategic Tools
  • From Compliance Pipes to Data Streams: Modernizing Healthcare EDI for Strategic Value
  • AI in Software Engineering: 3 Critical Mistakes to Avoid (and What to Do Instead)

Trending

  • GenAI Implementation Isn't Magic — It’s a Lifecycle
  • Jakarta EE 12: Entering the Data Age of Enterprise Java
  • Persistent Memory for AI Agents Using LangChain's Deep Agents
  • Liquid Glass, Material 3, and a Lot of Plumbing
  1. DZone
  2. Coding
  3. JavaScript
  4. Logic Behind Software — allMatch On Empty Stream

Logic Behind Software — allMatch On Empty Stream

Have you ever wonder why all match on empty Stream will always return true ? Here you will get all information needed to understand why it works this way.

By 
Bartłomiej Żyliński user avatar
Bartłomiej Żyliński
DZone Core CORE ·
Nov. 09, 21 · Analysis
Likes (8)
Comment
Save
Tweet
Share
8.0K Views

Join the DZone community and get the full member experience.

Join For Free

Basically, it works this way because of the logic concept called Vacuous truth. It is a simple and short answer but because we do not like such answers, we will dive deeper. But why I even decided to write about such a niche topic. For some time, I have been interested in how mathematics affects the software engineering world directly, and I have even written an article about calculating scalability. I believe such niche topics can be quite an interesting thing for you.

We will start by shortly presenting the main problem described in today's text.

AllMatch On Empty Stream Returning True

Below you can find a code snippet needed to recreate the problem. However, I will explain the context first. I have always expected that calling allMatch on an empty Stream will return false because there are no possible elements to match, so naturally, none of the elements can match. This may have been an overengineered understanding but nevertheless, it was what I thought.

Fortunately (or unfortunately), I was informed by my coworker from Softwaremill - Jakub Cichy that I was wrong. I must admit that I was quite surprised but after reading more about the topic it became clearer. That is how I learned about the existence of the concept known as Vacuous truth.

Java
public class Main {

  public static void main(String[] args) {
      boolean b = Stream.empty().allMatch(e -> e.equals("2"));
      System.out.println(b);
  }
}

As a side note, I can add that your IDE will mark the third line of the above snippet as compilation warning. The message will state thatallMatch on empty Stream will always be true – at least it was the case in IntelliJ IDEA.

What Vacuous Truth Is And How It Works

In general, it is a logical concept used to describe all statements that are evaluated as truth because their base condition (Antecedent) cannot be fulfilled. What it exactly means is that in the process of evaluation of every logic statement, the first half of a hypothetical proposition is responsible for deciding if evaluation of a particular statement will precede to then-clause or not — this is what antecedent is.

For example, in the statement “all people reading this are developers” an antecedent is an implicit condition that this article will be read by anyone, so if this article gets 0 views the above statement will be automatically evaluated as being true.

The term Vacuous Truth can be also used to describe statements with false base condition, for example: if Christmas is in January you will get no gifts. In both cases, false antecedents make it impossible for readers to reason about the output of then-clause of the statement.

Vacuous Truth In Real Life

In math, Vacuous Truth statements are not the main point of interest themselves but they are often used in proofs based on mathematical inductions.

Outside the mathematical context, Vacuous Truth statements can be described as misleading. In most cases, such statements provide reasonable information about real-life objects which do not exist in particular situations.

To some extent, all fake news can be categorized as Vacuous Truth statements.

Vacuous Truth in Software Engineering

Except the already provided Java example, similar cases can be recreated in many other modern-day programming languages:

  1. In Scala method forall, present in many interfaces of collections, works in the same way.
  2. In JavaScript method every from array prototype is implemented similarly.
  3. In Python built-in function all, which tests if all elements of array are true, will also return true for empty array.

Summary

I presented the problem and provided a code snipped needed to recreate it, then I described what the logic concept behind its occurrence is. Additionally, I explained what Vacuous Truth is and what use cases it has in real life. I also mentioned some other methods with similar inner workings present in languages other than Java. I hope that my article was interesting or maybe even surprising and that it will help you somewhere along your path as a software engineer. Thank you for your time.

Software engineering Stream (computing)

Opinions expressed by DZone contributors are their own.

Related

  • Alternative Structured Concurrency
  • Designing Effective Meetings in Tech: From Time Wasters to Strategic Tools
  • From Compliance Pipes to Data Streams: Modernizing Healthcare EDI for Strategic Value
  • AI in Software Engineering: 3 Critical Mistakes to Avoid (and What to Do Instead)

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook