At IDR Solutions we use Java 8 for the development of our products (a Java PDF Viewer and SDK, PDF to HTML5 converter and a Java ImageIO replacement). In this article, I will be looking at the Consumer Supplier.
What are these?
These features are functional interfaces (an interface with only one abstract method) that belong 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 let’s have a look at 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();
}
Let’s again have a look at 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
If you found this article useful, you may also be interested in our other Java8 posts on Lambda Expression, Streams API, Default Methods, Method References, Optional and Repeating Annotations.
We also now have a large collection of articles on what is new in other versions of Java, including Java9, and Java13.
Our software libraries allow you to
Convert PDF to HTML in Java |
Convert PDF Forms to HTML5 in Java |
Convert PDF Documents to an image in Java |
Work with PDF Documents in Java |
Read and Write AVIF, HEIC, WEBP and other image formats |
Thanks, nice explanation
I think you need to use
Consumer consumer = ConsumerTest::printNames;
in the Consumer example in order to have it compiling.
The method printNames() should probably be called printName() since it prints only one name per call.
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).
Hi Paul,
Thank you for pointing this out to us, I have adjusted the code and it should be working fine now.
Consumer consumer = (x) -> printNames(x);
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….
Hello Clark i got the same with Eclipse
You need to type cast the consumer, to make it work .
Consumer consumer = ConsumerTest::printNames;
Consumer > String < consumer = ConsumerTest::printNames;
Hi, replace this to make it works in the Consumer example:
private static void printNames(Object name) {
System.out.println(name);
}
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);
}
}
Thx for the example 🙂
Consumer must be of type String, if method reference is used:
Consumerconsumer = ConsumerTest::printNames;
How to make a consumer to save the input parameter to a database?
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);
}
}