Java 8 Consumer example

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.

Consumer definition

Consumer takes single argument and do not return any result.
Here is the definition of Consumer interface.

It has a functional method called accept() and default method andThen().

Consumer examples

accept() method example

Let’s use Consumer interface to print String:

We have created consumer object which takes String object as input and print it.It is simple use of Consumer interface to print String.
When you run above program, you will get below output:

Arpit

default andThen() method example

As per java docs:
Returns a composed Consumer that performs, in sequence, this operation followed by the after operation. If performing either operation throws an exception, it is relayed to the caller of the composed operation. If performing this operation throws an exception, the after operation will not be performed.

Output:

arpit
ARPIT

As you can see, we have created two consumers and used andThen() method to create composite consumer. When we called accept() method on composite consumer, both the consumers are called in sequence.

Consumer foreach example

Let’s say you have Student class as below:

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

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

name : Arpit == Age : 19
name : John == Age : 17
name : Mary == Age : 14
name : Martin == Age : 21
name : Monica == Age : 16
name : Ally == Age : 20

We have created consumer object which takes student object as input and print it.Above created consumer object is then passed to foreach() method of Iterable interface.

As you can see, foreach() method takes consumer as input and calls accept() method on each object of Iterable.
That’s all about Java 8 consumer example.


import_contacts

You may also like:

Related Posts

  • Convert Date to LocalDate in java
    12 January

    Java Date to LocalDate

    In this post, we will see how to convert Date to LocalDate in java. Sometimes, we may need to convert Date to new Java 8 APIs and vice versa. There are multiple ways to convert Date to LocalDate in java. Read also: Convert LocalDate to Date in java Using toInstant() method of Date class You […]

  • Convert LocalDate to Date in java
    11 January

    Java LocalDate to Date

    In this post, we will see how to convert LocalDate to Date. Java 8 has introduced a lot of new APIs for Date and time. There can be many ways to convert Java LocalDateTime to date. Using Instant object You can convert LocalDate to Date using Instant object which we can from Zone. Here is […]

  • 31 December

    Java Stream to List

    In this post, we will see how to convert Stream to List in java. There are multiple ways to convert Stream to List in java. Using Collectors.toList() You can pass Collectors.toList() to Stream.collect() method to convert Stream to List in java. Stream’s collect method performs mutable reduction operation on elements of Stream and Collectors.toList() provides […]

  • 18 November

    Convert LocalDateTime to Timestamp in Java

    In this post, we will how to convert LocalDateTime to Timestamp . Before learning how to convert localdatetime to timestamp, let us learn about LocalDateTime and Timestamp, and understand the importance of this conversion. LocalDateTime LocalDateTime was introcuded in Java 8. LocalDateTime can be imported time package: import java.time.LocalDateTime; LocalDateTime is an immutable object used […]

  • 28 August

    A In-Depth guide to Java 8 Stream API

    In this post, we will see an in-depth overview of Java 8 streams with a lot of examples and exercises. Introduction You may think that Stream must be similar to InputStream or OutputStream, but that’s not the case. A Stream represents a sequence of elements supporting sequential and parallel aggregate operations. Stream does not store […]

  • 26 April

    Java Stream List to Map

    In this post, we will see how to convert List to Map using Stream in java 8. Collectors’s toMap() can be used with Stream to convert List to Map in java. Consider a class Named Movie which have 3 fields – id, name and genre [crayon-60026709aec13386208924/] Create a list of movies and convert with to […]

Leave a Reply

Your email address will not be published. Required fields are marked *

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.