HowToDoInJava

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

Java chained predicates – logical AND and logical OR operations

By Lokesh Gupta | Filed Under: Java 8

Java examples of chained predicates and to perform ‘logical AND‘ and ‘logical OR‘ operations and collect the elements into a list.

1. Predicate.and() – Logical AND example

In given example, we have used Predicate.and() method which returns a composed predicate that represents a short-circuiting logical AND of this predicate and another.

When evaluating the composed predicate, if first predicate is false, then the other predicate is not evaluated.

Any exceptions thrown during evaluation of either predicate are relayed to the caller; if evaluation of first predicate throws an exception, the other predicate will not be evaluated.

import java.util.Arrays;
import java.util.List;
import java.util.function.Predicate;
import java.util.stream.Collectors;

public class Main 
{
	public static void main(String[] args) 
	{
		List<Employee> employeesList = Arrays.asList(
											new Employee(1, "Alex", 100),
											new Employee(2, "Brian", 200),
											new Employee(3, "Charles", 300),
											new Employee(4, "David", 400),
											new Employee(5, "Edward", 500),
											new Employee(6, "Frank", 600)
										);
		
		Predicate<Employee> idLessThan4 = e -> e.getId() < 4;
		
		Predicate<Employee> salaryGreaterThan200 = e -> e.getSalary() > 200;

		List<Employee> filteredEmployees = employeesList.stream()
								.filter( idLessThan4.and( salaryGreaterThan200 ) )
								.collect(Collectors.toList());

		System.out.println(filteredEmployees);
	}
}

Program output.

[Employee [id=3, name=Charles, salary=300.0]]

2. Predicate.or() – Logical OR example

In given example, we have used Predicate.or() method which returns a composed predicate that represents a short-circuiting logical OR of given predicate and another predicate.

When evaluating the composed predicate, if first predicate is true, then the other predicate is not evaluated.

Any exceptions thrown during evaluation of either predicate are relayed to the caller; if evaluation of first predicate throws an exception, the other predicate will not be evaluated.

import java.util.Arrays;
import java.util.List;
import java.util.function.Predicate;
import java.util.stream.Collectors;

public class Main 
{
	public static void main(String[] args) 
	{
		List<Employee> employeesList = Arrays.asList(
											new Employee(1, "Alex", 100),
											new Employee(2, "Brian", 200),
											new Employee(3, "Charles", 300),
											new Employee(4, "David", 400),
											new Employee(5, "Edward", 500),
											new Employee(6, "Frank", 600)
										);
		
		Predicate<Employee> idLessThan2 = e -> e.getId() < 2;
		
		Predicate<Employee> salaryGreaterThan500 = e -> e.getSalary() > 500;

		List<Employee> filteredEmployees = employeesList.stream()
								.filter( idLessThan2.or( salaryGreaterThan500 ) )
								.collect(Collectors.toList());

		System.out.println(filteredEmployees);
	}
}

Program output.

[Employee [id=1, name=Alex, salary=100.0], 
Employee [id=6, name=Frank, salary=600.0]]

Drop me your questions related to Java stream chained predicates equivalent to logical operators between predicates.

Happy Learning !!

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