[Java] Element existence check with Stream

There is no method called Stream # isEmpty, so I don't know how to write it best, so I write it in Qiita.

First of all, in normal times, I would write as follows.

list.stream()
        .anyMatch(Condition lambda expression or method reference);

But this time, how should I write if I just want to make a judgment like List # isEmpty for Stream? It is a story.

For example, I just want to judge whether there is a result in Stream that was returned by throwing a query in Doma2. May use.

As a result, I don't know the answer which is the best, but I write the pattern I thought about.

Always true with anyMatch

It may be a little scary at first glance.

	boolean notEmpty = stream()
			.anyMatch(e -> true);

	System.out.println(notEmpty);

Iterator#hasNext

Impression that you do not know what you want to do at a glance without explanatory variables.

	boolean notEmpty = stream()
			.iterator()
			.hasNext();

	System.out.println(notEmpty);

Optional#isPresent

Hmm.

	boolean notEmpty = stream()
			.findAny()
			.isPresent();

	System.out.println(notEmpty);

count

NG because there is a lot of waste.

	boolean notEmpty = stream()
			.count() > 0;

	System.out.println(notEmpty);

Count to be 1 or 0

This is still better.

	//Still better
	boolean notEmpty = stream()
			.limit(1)
			.count() > 0;

	System.out.println(notEmpty);

toList

This is also wasteful, so NG.

	boolean notEmpty = !stream()
			.collect(Collectors.toList())
			.isEmpty();

	System.out.println(notEmpty);

Isn't the pattern of ʻIterator # hasNext good for efficiency? (Especially if used with Doma2) If I was in that scene, I would write it with ʻany Match (e-> true), which seems to be the easiest without thinking about anything.

Recommended Posts

[Java] Element existence check with Stream
[Java 8] Duplicate deletion (& duplicate check) with Stream
Java8 list conversion with Stream map
Check heap usage with Java Flight Recorder
[JAVA] Stream type
Try Java 8 Stream
Java version check
Java Stream API
Java check process
Studying Java 8 (Stream)
Java Stream termination
[Java] Stream processing
Java 9 Optional :: stream
[Java] Create a collection with only one element
Make Java Stream line breaks nice with eclipse
Java8 Stream duplicate check (write neatly using Collector)
Install java with Homebrew
[Java] Stream Collectors notes
Check Java toString () implementation
Change seats with java
Install Java with Ansible
[Java] Stream API-Stream generation
[Java] Stream API / map
Check the behavior of Java Intrinsic Locks with bpftrace
Switch java with direnv
Java8 Stream API practice
Java8 Stream reduction operation
Download Java with Ansible
Let's scrape with Java! !!
Getting Started with Legacy Java Engineers (Stream + Lambda Expression)
Check coverage with Codecov in Java + Gradle + Wercker configuration
Build Java with Wercker
Output FizzBuzz with stream
Java8 Stream Rough Summary
Endian conversion with JAVA
How to handle exceptions coolly with Java 8 Stream or Optional
Handle exceptions coolly with Java 8 lambda expressions and Stream API
Rethinking design patterns with Java8 lambda expressions & Stream --Builder pattern -
List processing to understand with pictures --java8 stream / javaslang --bonus
[Java] Check the difference between orElse and orElseGet with IntStream
Convert 2D array to csv format with Java 8 Stream API
[Maven] Disable .Net-based Assembly Analyzer with OWASP Dependency Check [Java]
Easy BDD with (Java) Spectrum?
Use Lambda Layers with Java
Java multi-project creation with Gradle
Java Config with Spring MVC
Let's experiment with Java inlining
Check CSV value with RSpec
[Template] MySQL connection with Java
Rewrite Java try-catch with Optional
Install Java 7 with Homebrew (cask)
[Java] JSON communication with jackson
Java to play with Function
Try DB connection with Java
[Java11] Stream Summary -Advantages of Stream-
Enable Java EE with NetBeans 9
[Java] JavaConfig with Static InnerClass
Try gRPC with Java, Maven
Let's operate Excel with Java! !!
Version control Java with SDKMAN
Integer check method with ruby