Java 8 Consumer example

Java 8 Consumer
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:

Student [id=1, name=Arpit, gender=M, age=19] Student [id=2, name=John, gender=M, age=17] Student [id=3, name=Mary, gender=F, age=14] Student [id=4, name=Martin, gender=M, age=21] Student [id=5, name=Monica, gender=F, age=16] Student [id=6, name=Ally, gender=F, 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.

Was this post helpful?


import_contacts

You may also like:

Related Posts

  • Unable to obtain LocalDateTime from TemporalAccessor
    20 February

    [Fixed] Unable to obtain LocalDateTime from TemporalAccessor

    Table of ContentsUnable to obtain LocalDateTime from TemporalAccessor : ReasonUnable to obtain LocalDateTime from TemporalAccessor : FixLocalDate’s parse() method with atStartOfDay()Use LocalDate instead of LocalDateTime In this article, we will see how to fix Unable to obtain LocalDateTime from TemporalAccessor in Java 8. Unable to obtain LocalDateTime from TemporalAccessor : Reason You will generally get […]

  • Java LocalDate to Instant
    17 February

    Convert LocalDate to Instant in Java

    Table of ContentsJava LocalDate to InstantUsing toInstant() wth ZoneIdUsing toInstant() with ZoneOffset In this article, we will see how to convert LocalDate to Instant in Java. Java LocalDate to Instant Instant class provides an instantaneous point in time. When you want to convert LocalDate to Instant, you need to provide time zone. Using toInstant() wth […]

  • Java Instant to LocalDate
    17 February

    Convert Instant to LocalDate in Java

    Table of ContentsUsing ofInstant method [ Java 9+]Using ZoneDateTime’s toLocalDate() [Java 8] In this article, we will see how to convert Instant to LocalDate in java. Using ofInstant method [ Java 9+] Java 9 has introduced static method ofInstant() method in LocalDate class. It takes Instant and ZoneId as input and returns LocalDate object. [crayon-633054be89897156851340/] […]

  • Convert String to LocalDateTime in Java
    17 February

    Convert String to LocalDateTime in Java

    Table of ContentsJava String to LocalDateTimeConvert String to LocalDateTime with custom format In this article, we will see how to convert String to LocalDateTime in Java. LocalDateTime class was introduced in Java 8. LocalDateTime represents local date and time without timezone information. It is represented in ISO 8601 format (yyyy-MM-ddTHH:mm:ss) by default. Java String to […]

  • Java LocalDateTIme to String
    16 February

    Format LocalDateTime to String in Java

    Table of ContentsJava LocalDateTime To StringConvert LocalDateTime to Time Zone ISO8601 StringParse String to LocalDateTime In this article, we will see how to format LocalDateTime to String in java. Java LocalDateTime To String To format LocalDateTime to String, we can create DateTimeFormatter and pass it to LocalDateTime’s format() method. [crayon-633054be8b6b6551734951/] Here are steps: Get LocalDateTime […]

  • Find duplicate elements in the Stream
    17 October

    Java 8 – Find duplicate elements in Stream

    Table of ContentsIntroductionUsing distinct()Using Collections.frequency()Using Collectors.toSet()Using Collectors.toMap()Using Collectors.groupingBy()Conclusion Introduction When working with a collection of elements in Java, it is very common to have duplicate elements, and Java provides different APIs that we can use to solve the problem. Java 8 Stream provides the functionality to perform aggregate operations on a collection, and one of […]

Leave a Reply

Your email address will not be published.

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.