Java 8 Consumer example

Previous
Next
In this post, we are going to see about java 8 Consumer interface.
Consumer is single argument functional interface like Predicate but it does not return any value. As Consumer is functional interface, so it  can be used as assignment target for lambda expressions.
It has a functional method called accept(Object).
Lets use Consumer interface to print String:

It is simple use of Consumer interface to print String. When you run above program, you will get below output:

Example: Lets say you have student class as below:

Now lets create Consumer object in main class and pass it to forEach method of list:

When you run above program, you will get below output:

Previous
Next

Join Our News Letter - Stay Updated

Subscribe to Awesome Java Content.

Add Comment

Join Our News Letter - Stay Updated

Subscribe to Awesome Java Content.
You can like our facebook page Java2blog