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
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Last call! Secure your stack and shape the future! Help dev teams across the globe navigate their software supply chain security challenges.

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Releasing software shouldn't be stressful or risky. Learn how to leverage progressive delivery techniques to ensure safer deployments.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

Related

  • Why Haven’t You Upgraded to HTTP/2?
  • Advanced Brain-Computer Interfaces With Java
  • Simplify Java: Reducing Unnecessary Layers and Interfaces [Video]
  • Mastering Backpressure in Java: Concepts, Real-World Examples, and Implementation

Trending

  • Teradata Performance and Skew Prevention Tips
  • Optimizing Integration Workflows With Spark Structured Streaming and Cloud Services
  • The Role of Functional Programming in Modern Software Development
  • Apache Doris vs Elasticsearch: An In-Depth Comparative Analysis
  1. DZone
  2. Coding
  3. Java
  4. Predicate and Consumer Interface in java.util.function package in Java 8

Predicate and Consumer Interface in java.util.function package in Java 8

Here's how to properly use the Predicate and Consumer interfaces in Java 8.

By 
Mohamed Sanaulla user avatar
Mohamed Sanaulla
·
Apr. 11, 13 · Tutorial
Likes (17)
Comment
Save
Tweet
Share
39.3K Views

Join the DZone community and get the full member experience.

Join For Free

In my previous post I wrote about Function interface which is part of java.util.package. I also mentioned about Predicate interface which is part of the same package and in this post I will show you how you can make use of the Predicate and Consumer interfaces.

Lets look at the Javadoc for Predicate interface:

Determines if the input object matches some criteria.

And there are 5 methods declared/defined in that interface (you must be wondering how this is a functional interface, if you are then you must read this before proceeding) and those methods are:

//Returns a predicate which evaluates to true only if this predicate 
//and the provided predicate both evaluate to true.
and(Predicate<? super T> p)

//Returns a predicate which negates the result of this predicate.
negate()

//Returns a predicate which evaluates to true if either 
//this predicate or the provided predicate evaluates to true
or(Predicate<? super T> p)

//Returns true if the input object matches some criteria
test(T t)

//Returns a predicate that evaluates to true if both or neither 
//of the component predicates evaluate to true
xor(Predicate<? super T> p)

All of the methods except test(T t) are default methods and test(T t) is abstract. One approach of providing the implementation for this abstract method is to use Anonymous inner class and the other approach is to use lambda expression.

The Javadoc for Consumer interface states:

An operation which accepts a single input argument and returns no result. Unlike most other functional interfaces, Consumer is expected to operate via side-effects.

There are 2 methods in this interface out of which only one is abstract and that abstract method is: accept(T t), which accepts an input and doesn’t return any result.

To explain more about Predicate and Consumer interface lets consider a Student class with name, grade and fee to be paid. Each student has some discount which is decided based on the Student’s grade.

class Student{
  String firstName;
  String lastName;
  Double grade;
  Double feeDiscount = 0.0;
  Double baseFee = 20000.0;

  public Student(String firstName, String lastName, 
                 Double grade) {
    this.firstName = firstName;
    this.lastName = lastName;
    this.grade = grade;
  }

  public void printFee(){
    Double newFee = baseFee - ((baseFee*feeDiscount)/100);
    System.out.println("The fee after discount: "+newFee);
  }
}

And lets create a method which accepts a Student object, a Predicate implementation and a Consumer implementation. If you are not familiar with Function interface, then you should spent few minutes reading this. This method uses the predicate to decide if the student discount on the fee has to be updated and then uses the Consumer implementation to update the discount.

public class PreidcateConsumerDemo {

  public static Student updateStudentFee(Student student, 
          Predicate<Student> predicate, 
          Consumer<Student> consumer){

    //Use the predicate to decide when to update the discount.
    if ( predicate.test(student)){
      //Use the consumer to update the discount value.
      consumer.accept(student);
    }
    return student;
  }

}

Both the test method and the accept method in the Predicate and Consumer respectively accept a parameter of the generic type declared. The difference between these is that the predicate uses the parameter to make some decision and return a boolean whereas Consumer uses the parameter to change some of its value.
Lets look at how the updateStudentFee method is invoked:

public static void main(String[] args) {

  Student student1 = new Student("Ashok","Kumar", 9.5);
  student1 = updateStudentFee(student1,
                      //Lambda expression for Predicate interface
                      student -> student.grade > 8.5, 
                      //Lambda expression for Consumer inerface
                      student -> student.feeDiscount = 30.0);
  student1.printFee();

  Student student2 = new Student("Rajat","Verma", 8.0);
  student2 = updateStudentFee(student2, 
                      student -> student.grade >= 8,
                      student -> student.feeDiscount = 20.0);
  student2.printFee();

}

In this post I explained with a sample how we can make use of Predicate and Consumer interfaces which are part of the java.util.function package to be introduced in Java 8.




 

Interface (computing) consumer Java (programming language)

Published at DZone with permission of Mohamed Sanaulla, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Why Haven’t You Upgraded to HTTP/2?
  • Advanced Brain-Computer Interfaces With Java
  • Simplify Java: Reducing Unnecessary Layers and Interfaces [Video]
  • Mastering Backpressure in Java: Concepts, Real-World Examples, and Implementation

Partner Resources

×

Comments
Oops! Something Went Wrong

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

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

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 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!