GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Private Policy
  • Contact
  • Java
  • Java Core
  • Java Lambda Expression
  • Java Fundamentals
  • Java Stream
  • Java Tools
  • Java XML
  • Jakarta EE
  • CDI
  • JSON Binding
  • JSON Processing
  • MVC

This view

  • Original page
  • New address

GeezerVison is showing huongdanjava.com — Learn about Supplier Functional Interface in Java - Huong Dan Java

Khanh Nguyen

I love Java and everything related to Java.

Follow

Share this post

Tags

  • Java Supplier
Views: 1,634

Supplier is a Functional Interface, so it only contains an abstract method as follows:

Java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
package java.util.function;
 
/**
* Represents a supplier of results.
*
* <p>There is no requirement that a new or distinct result be returned each
* time the supplier is invoked.
*
* <p>This is a <a href="package-summary.html">functional interface</a>
* whose functional method is {@link #get()}.
*
* @param <T> the type of results supplied by this supplier
*
* @since 1.8
*/
@FunctionalInterface
public interface Supplier<T> {
 
    /**
     * Gets a result.
     *
     * @return a result
     */
    T get();
}

This interface simply returns the value in the context that is being used by the get() method.

For example, we have the following list:

Java
1
List<String> s = Arrays.asList("Khanh", "Quan");

Now that I want to use the Supplier interface to get the value of each element in the list and print out to the console, I will write a method like this:

Java
1
2
3
private void print(Supplier supplier) {
    System.out.println(supplier.get());
}

And now we can use this method:

Java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
package com.huongdanjava.javaexample;
 
import java.util.Arrays;
import java.util.List;
import java.util.function.Supplier;
 
public class Example {
 
public static void main(String[] args) {
List<String> s = Arrays.asList("Khanh", "Quan");
 
s.stream().forEach(x -> print(() -> x));
}
 
private static void print(Supplier supplier) {
System.out.println(supplier.get());
}
 
}

In the above code, instead of using the Consumer interface, I used the print() method with the Supplier interface parameter, which is the value of each element in the list.

Result:

Learn about Supplier Functional Interface in Java

Like the Consumer interface, here are some variations of the Supplier interface:

  • Interface IntSupplier: The getAsInt() method returns the Integer value.
  • Interface DoubleSupplier: with the getAsDouble() method returns a Double value.
  • LongSupplier interface: with the getAsLong() method returns a Long value.
  • Interface BooleanSupplier: The getAsBoolean() method returns the Integer value.

You might also like...

  • What is a Database Transaction? ACID, JDBC & Isolation Levels

    What is a Database Transaction? ACID, JDBC & Isolation Levels

  • What is JDBC? JDBC Architecture, Driver, Connection, Statement & PreparedStatement | Java

    What is JDBC? JDBC Architecture, Driver, Connection, Statement & PreparedStatement | Java

  • Java Primitive Data Types Part 2 | float, double, boolean

    Java Primitive Data Types Part 2 | float, double, boolean

  • Java Primitive Data Types Part 1 | byte, short, int, long, char & Type Conversion

    Java Primitive Data Types Part 1 | byte, short, int, long, char & Type Conversion

Previous Post: Learn about Consumer Functional Interface in Java
Next Post: Or() methods of Optional object in Java

Add Comment Cancel reply

You are visitor 01402520 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.