HowToDoInJava

  • Java 8
  • Regex
  • Concurrency
  • Best Practices
  • Spring Boot
  • JUnit5
  • Interview Questions
  • Dark Mode

Java Stream anyMatch() API

By Lokesh Gupta | Filed Under: Java 8

Java Stream anyMatch (Predicate predicate) is terminal-short-circuiting operation which is used to check if the stream contains any matching element with provided predicate.

1. Stream anyMatch() method

1.1. Syntax

boolean anyMatch(Predicate<? super T> predicate)

Here predicate a non-interfering, stateless predicate to apply to elements of the stream.

1.2. Description

  • It is a short-circuiting terminal operation.
  • It returns whether any elements of this stream match the provided predicate.
  • May not evaluate the predicate on all elements if not necessary for determining the result. Method returns true as soon as first matching element is encountered.
  • If the stream is empty then false is returned and the predicate is not evaluated.

2. Java Stream anyMatch() example

Java example of Stream.anyMatch() method to check if any stream element match the method argument predicate.

import java.util.stream.Stream;

public class Main 
{
	public static void main(String[] args) 
	{
		Stream<String> stream = Stream.of("one", "two", "three", "four");
		
		boolean match = stream.anyMatch(s -> s.contains("four"));
		
		System.out.println(match);		//true
	}
}

Program output.

true

3. Difference between anyMatch() vs contains()

Theoretically, there is no difference between anyMatch() and contains() when we want to check if an element exist in a list.

Parallelism might bring an advantage for really large lists, but we should not casually use the Stream.parallel() every time and there assuming that it may make things faster. In fact, invoking parallel() may bring down the performance for small streams.

4. Conclusion

Stream.anyMatch() method can be a useful in certain cases where we need to check if there is at least one element in the stream.

The shorter version list.contains() also does the same thing and can be used instead.

Reference :

Java Stream Interface

TwitterFacebookLinkedinRedditPocket

About Lokesh Gupta

A family guy with fun loving nature. Love computers, programming and solving everyday problems. Find me on Facebook and Twitter.

Leave a Reply

This comment form is under antispam protection
This comment form is under antispam protection
  Subscribe  
Notify of

Search Tutorials

Java 8 Tutorial

  • Java 8 – Introduction
  • Java 8 – forEach
  • Java 8 – Stream
  • Java 8 – Boxed Stream
  • Java 8 – Lambda Expression
  • Java 8 – Functional Interface
  • Java 8 – Method References
  • Java 8 – Default Method
  • Java 8 – Optionals
  • Java 8 – Predicate
  • Java 8 – Date Time
  • Java 8 – Iterate Directory
  • Java 8 – Read File
  • Java 8 – Write to File
  • Java 8 – WatchService
  • Java 8 – String to Date
  • Java 8 – Join Array
  • Java 8 – Base64
  • Java 8 – Exact Arithmetic
  • Java 8 – Comparator
  • Java 8 – Regex as Predicate
  • Java 8 – Join String
  • Java 8 – Difference Between Dates
  • Internal vs. External Iteration
  • Java 8- SecureRandom

Popular Tutorials

  • Java 8 Tutorial
  • Core Java Tutorial
  • Collections in Java
  • Java Concurrency
  • Spring Boot Tutorial
  • Spring AOP Tutorial
  • Spring MVC Tutorial
  • Spring Security Tutorial
  • Hibernate Tutorial
  • Python Tutorial
  • Jersey Tutorial
  • Maven Tutorial
  • Log4j Tutorial
  • Regex Tutorial

Meta Links

  • Advertise
  • Contact Us
  • Privacy policy
  • About Me

Recommended Reading

  • 10 Life Lessons
  • Secure Hash Algorithms
  • How Web Servers work?
  • How Java I/O Works Internally?
  • Best Way to Learn Java
  • Java Best Practices Guide
  • Microservices Tutorial
  • REST API Tutorial
  • How to Start New Blog

Copyright © 2016 · HowToDoInjava.com · All Rights Reserved. | Sitemap

wpDiscuz