Ernest Duodu Ernest is a developer at IDRSolutions. He focuses mainly on our JavaFX PDF Viewer implementation. He was a speaker at JavaOne 2014, where he did a joint session titled "Lessons learnt developing a NetBeans PDF viewer plugin in JavaFX". Aside programming, he also enjoys a wide variety of hobbies which includes sky-diving, photography, exercising and listening to music.

Java 8 Consumer Supplier Explained in 5 minutes

1 min read

At IDR Solutions we use Java 8 for the development of our products (a Java PDF Viewer and SDKPDF to HTML5 converter and a Java ImageIO replacement).

In my previous Java 8 series articles, we discussed Lambda Expression, Streams API, Default Methods and Method References. and Repeating Annotations. This time we will be looking at Consumer Supplier.

What are these?

These features are functional interfaces (an interface with only one abstract method) which belongs to the java.util.function package.

The consumer accepts a single argument by calling its accept (args) method and does not return any value making it a void method.

@FunctionalInterface
public interface Consumer {
  void accept(T t);
}

Now Lets have a look of a simple code example where the Consumer interface is being used.

import java.util.function.Consumer;
 
public class ConsumerTest {
 
 
 
    public static void main(String[] args) {
 
 
Consumer consumer = ConsumerTest::printNames;
 
 
consumer.accept("Jeremy");
consumer.accept("Paul");
consumer.accept("Richard");
 
    } 
 
    private static void printNames(String name) {
        System.out.println(name);
    }
}

Jeremy
Paul
Richard

The supplier does the opposite of the consumer, so it takes no arguments but it returns some value by calling its get() method.
Note: This may return different values when it is being called more than once.

@FunctionalInterface
public interface Supplier {
  T get();
}

Lets again have a look of a simple code example where the Supplier interface is being used.

import java.util.ArrayList;
import java.util.List;
import java.util.function.Supplier;
 
public class SupplierTest {
 
 
 
    public static void main(String[] args) {
 
 
        List  names = new ArrayList(); 
        names.add( "David");
        names.add( "Sam");
   names.add( "Ben");
 
        names.stream().forEach((x) -> {
            printNames(() -> x);
        });
 
    }
 
      static void printNames(Supplier arg) {
System.out.println(arg.get());
    }
}

David
Sam
Ben

Hopefully you have found this quick guide useful.

We now have series of articles on what is new in Java 9 and Java 10.

IDRsolutions develop a Java PDF Viewer and SDK, an Adobe forms to HTML5 forms converter, a PDF to HTML5 converter and a Java ImageIO replacement. On the blog our team post anything interesting they learn about.

Ernest Duodu Ernest is a developer at IDRSolutions. He focuses mainly on our JavaFX PDF Viewer implementation. He was a speaker at JavaOne 2014, where he did a joint session titled "Lessons learnt developing a NetBeans PDF viewer plugin in JavaFX". Aside programming, he also enjoys a wide variety of hobbies which includes sky-diving, photography, exercising and listening to music.

15 Replies to “Java 8 Consumer Supplier Explained in 5 minutes”

  1. I think you need to use


    Consumer consumer = ConsumerTest::printNames;

    in the Consumer example in order to have it compiling.

  2. The greaterThan symbol used when declaring lambda for the forEach block is not displayed correctly (i.e. the characters for ampersand, g, t and semicolon are displayed).

  3. Eclipse: cannot compile
    @FunctionalInterface
    public interface Consumer {
    void accept(T t);
    }
    compiler error: T cannot be solved as a type

    Consumer consumer = ConsumerTest::printNames;
    compiler error:Multiple errors….

  4. Hi, replace this to make it works in the Consumer example:

    private static void printNames(Object name) {
    System.out.println(name);
    }

  5. import java.util.function.Consumer;

    public class ConsumerExample {
    public static void main(String[] args) {
    Consumer consumer = (x) -> printNames(x);
    consumer.accept(“Jeremy”);
    consumer.accept(“Paul”);
    consumer.accept(“Richard”);
    }

    private static void printNames(Object name) {
    System.out.println(name);
    }
    }

  6. Thx for the example 🙂
    Consumer must be of type String, if method reference is used:
    Consumerconsumer = ConsumerTest::printNames;

  7. import java.util.function.Consumer;

    public class ConsumerTest {

    public static void main(String[] args) {

    Consumer consumer = ConsumerTest::printNames;

    consumer.accept(“Jeremy”);
    consumer.accept(“Paul”);
    consumer.accept(“Richard”);
    }

    private static void printNames(String name) {
    System.out.println(name);
    }

    }

Leave a Reply

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