Java 8 Supplier example

Java 8 Supplier
In this post, we are going to see about java 8 Supplier interface.
Supplier is functional interface which does not take any argument and produces result of type T.It has a functional method called T get() As Supplier is functional interface, so it can be used as assignment target for lambda expressions.
Here is source code of Java 8 supplier interface.

Java 8 Supplier example

Lets use supplier interface to print String:

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

💻 Awesome Tech Resources:
  • Looking for ⚒️ tech jobs? Go to our job portal.
  • Looking for tech events? Go to tech events 🗓️ Calendar.️
Arpit

Let’s create another example using custom class Student.We will use supplier to supply new Student object.

Now lets create Supplier object which will be used to supply new Student object.

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

Student [id=1, name=Arpit, gender=M, age=19]

Use of supplier in Stream’s generate method

If you observe signature of Stream’s generate method, you will notice that it takes supplier as argument.

Stream's generate method returns an infinite sequential stream where supplier generates each element.
Let’s say you want to generate 5 random numbers between 0 to 10.

Output:

8
3
5
2
2

As you want see we have used supplier as () -> new Random().nextInt(10) to generate random numbers. limit(5) is used to limit the stream to 5 elements only and also used Java 8 foreach loop to print elements of the Stream.
That’s all about Java 8 Supplier example.


import_contacts

You may also like:

Related Posts

  • Format Instant to String in java
    02 May

    How to format Instant to String in java

    Learn about how to format Instant to String in java.

  • 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 💻 Awesome Tech Resources: Looking for ⚒️ […]

  • 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. 💻 Awesome Tech Resources: Looking for ⚒️ tech jobs? Go to our job portal. Looking for tech events? Go […]

  • Convert Stream to List in java
    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 […]

  • Convert LocalDateTime to Timestamp in java
    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; 💻 Awesome Tech Resources: Looking for […]

  • 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 […]

Comments

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.