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
Newsletter
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Java Stream API: 3 Things Every Developer Should Know About
  • Optimizing Java Applications: Parallel Processing and Result Aggregation Techniques
  • Functional Approach To String Manipulation in Java
  • Techniques You Should Know as a Kafka Streams Developer

Trending

  • PostgreSQL vs Oracle: When Higher Cost Means Better Performance
  • Android Application Security: Android Application Architecture
  • Grounding AI Agents in Governed Data
  • How to Format Articles for DZone

Draft This article hasn't been published, so readers can't see it.

In moderation This article is waiting for review, so readers can't see it yet.

Deleted This article has been deleted, so readers can't see it.

  1. DZone
  2. Data Engineering
  3. Databases
  4. Single vs. Multiple Filters in the Java Stream API

Single vs. Multiple Filters in the Java Stream API

It might be tempting to run multiple filters in your streams, but be careful—it might come with a cost. Use your filters judiciously.

By 
A N M Bazlur Rahman
DZone Core CORE ·
Dec. 29, 16 · Analysis
Post on X (opens in a new tab) Share on LinkedIn (opens in a new tab)
116.1K Views

Join the DZone community Save articles, join the discussion, and download Refcards and Trend Reports with a free account.

Join for free Already a member? Log in

One of the key features of Java 8 is the stream. It is frequently used in conjunction with lambdas, and one of them is the filter.

Let's consider the following example:

long count = doubles
   .stream()
   .filter(d -> d < Math.PI)
   .filter(d -> d > Math.E)
   .filter(d -> d != 3.10040970053377777)
   .filter(d -> d != 2.96240970053377777)
   .count();


It doesn't do anything fancy—perhaps it has no practical use case. However, for now, let's consider how the filter works. Each filter() method returns a new stream, so there in effect four extra steam.

However, of the four filters that can be written, one which has slightly less overhead. Let's compare these two ideas and see much benefit we can derive. 


import org.openjdk.jmh.annotations.Benchmark;
import org.openjdk.jmh.annotations.BenchmarkMode;
import org.openjdk.jmh.annotations.Mode;
import org.openjdk.jmh.annotations.OutputTimeUnit;

import java.util.List;
import java.util.Random;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

public class MyBenchmark {

	@Benchmark
	@BenchmarkMode(Mode.All)
	@OutputTimeUnit(TimeUnit.SECONDS)
	public long testStreamWithSingleFilter() {
		List<Double> doubles = new Random().doubles(1_000, 1, 4).boxed().collect(Collectors.toList());
		long count = doubles
			.stream()
			.filter(d -> d < Math.PI
				&& d > Math.E
				&& d != 3.10040970053377777
				&& d != 2.96240970053377777)
			.count();
		
		return count;
	}

	@Benchmark
	@BenchmarkMode(Mode.All)
	@OutputTimeUnit(TimeUnit.SECONDS)
	public long testStreamWithMultipleFilter() {
		List<Double> doubles = new Random().doubles(1_000, 1, 4).boxed().collect(Collectors.toList());
		long count = doubles
			.stream()
			.filter(d -> d > Math.E)
			.filter(d -> d < Math.PI)
			.filter(d -> d != 3.10040970053377777)
			.filter(d -> d != 2.96240970053377777)
			.count();
		
		return count;
	}
}

Output: 

# Run complete. Total time: 00:40:19

Benchmark                                                                        Mode      Cnt      Score     Error  Units
MyBenchmark.testStreamWithMultipleFilter                                        thrpt      200  24367.016 ± 169.686  ops/s
MyBenchmark.testStreamWithSingleFilter                                          thrpt      200  32779.157 ± 127.938  ops/s
MyBenchmark.testStreamWithMultipleFilter                                         avgt      200     ≈ 10⁻⁴             s/op
MyBenchmark.testStreamWithSingleFilter                                           avgt      200     ≈ 10⁻⁵             s/op
MyBenchmark.testStreamWithMultipleFilter                                       sample  2581418     ≈ 10⁻⁴             s/op
MyBenchmark.testStreamWithMultipleFilter:testStreamWithMultipleFilter·p0.00    sample              ≈ 10⁻⁴             s/op
MyBenchmark.testStreamWithMultipleFilter:testStreamWithMultipleFilter·p0.50    sample              ≈ 10⁻⁴             s/op
MyBenchmark.testStreamWithMultipleFilter:testStreamWithMultipleFilter·p0.90    sample              ≈ 10⁻⁴             s/op
MyBenchmark.testStreamWithMultipleFilter:testStreamWithMultipleFilter·p0.95    sample              ≈ 10⁻⁴             s/op
MyBenchmark.testStreamWithMultipleFilter:testStreamWithMultipleFilter·p0.99    sample              ≈ 10⁻⁴             s/op
MyBenchmark.testStreamWithMultipleFilter:testStreamWithMultipleFilter·p0.999   sample               0.001             s/op
MyBenchmark.testStreamWithMultipleFilter:testStreamWithMultipleFilter·p0.9999  sample               0.001             s/op
MyBenchmark.testStreamWithMultipleFilter:testStreamWithMultipleFilter·p1.00    sample               0.006             s/op
MyBenchmark.testStreamWithSingleFilter                                         sample  3292270     ≈ 10⁻⁵             s/op
MyBenchmark.testStreamWithSingleFilter:testStreamWithSingleFilter·p0.00        sample              ≈ 10⁻⁵             s/op
MyBenchmark.testStreamWithSingleFilter:testStreamWithSingleFilter·p0.50        sample              ≈ 10⁻⁵             s/op
MyBenchmark.testStreamWithSingleFilter:testStreamWithSingleFilter·p0.90        sample              ≈ 10⁻⁵             s/op
MyBenchmark.testStreamWithSingleFilter:testStreamWithSingleFilter·p0.95        sample              ≈ 10⁻⁵             s/op
MyBenchmark.testStreamWithSingleFilter:testStreamWithSingleFilter·p0.99        sample              ≈ 10⁻⁴             s/op
MyBenchmark.testStreamWithSingleFilter:testStreamWithSingleFilter·p0.999       sample              ≈ 10⁻⁴             s/op
MyBenchmark.testStreamWithSingleFilter:testStreamWithSingleFilter·p0.9999      sample               0.001             s/op
MyBenchmark.testStreamWithSingleFilter:testStreamWithSingleFilter·p1.00        sample               0.011             s/op
MyBenchmark.testStreamWithMultipleFilter                                           ss       10      0.010 ±   0.001   s/op
MyBenchmark.testStreamWithSingleFilter                                             ss       10      0.009 ±   0.001   s/op

As you can see, the single filter took less time than using multiple ones.

Source code: here

The takeaway: Multiple filters have some overhead; make sure to write good filters.

Filter (software) API Stream (computing) Java (programming language)

Opinions expressed by DZone contributors are their own.

Related

  • Java Stream API: 3 Things Every Developer Should Know About
  • Optimizing Java Applications: Parallel Processing and Result Aggregation Techniques
  • Functional Approach To String Manipulation in Java
  • Techniques You Should Know as a Kafka Streams Developer

Partner Resources

×

Comments

Loading comments

The comments didn't load

Please try again in a moment.

Likes

Loading

The next likes didn't load. Please try again.

The likes didn't load

Please try again in a moment.

See who liked this

Log in or join DZone for free to see who liked this article.

Log in Join for free
  • 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