#
Consumers in Java
A Functional Interface
is an interface which contains exactly one abstract method
.
It can have any number of default, static methods, but it can contain ONLY one abstract method.
Lambda expression is used to implement a functional interface abstract method.
Functional Interfaces and lambda expression are introduced in Java 8.
The Consumers are implementations of the Consumer<T>
functional interfaces.
In my example, I start from a simple Spring Boot application created using Spring initializr.
For my example I create an Employee class first:
package com.exampe.java;
public class Employee {
int id;
String name;
public Employee(int id, String name) {
this.id = id;
this.name = name;
}
public void setId(int id) {
this.id = id;
}
public int getId() {
return id;
}
public void setName(String name) {
this.name = name;
}
public String getName() {
return name;
}
}
After that we can create a Consumer<Employee>
implementation:
package com.exampe.java;
import java.util.function.Consumer;
public class Consumer1 implements Consumer<Employee> {
public void accept(Employee emp) {
System.out.println("Name = "+emp.getName());
}
}
And below we can see how the consumers are used:
package com.exampe.java;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import java.util.function.Consumer;
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
System.out.println("--------------------------------------------");
Employee emp = new Employee(10, "John");
// Consumer using a class
Consumer1 cons1 = new Consumer1();
cons1.accept(emp);
// Consumer using lambda expression
Consumer<String> cons2 = (String str1) -> {
System.out.println("You used as input parameter the following string: \""+str1+"\".");
};
cons2.accept("My string");
// Consumer using an inner class
Consumer<String> cons3 = new Consumer<String>() {
public void accept(String str1) {
System.out.println("You used as input parameter the following string: \""+str1+"\".");
}
};
cons3.accept("String2");
System.out.println("--------------------------------------------");
System.out.println("End of the execution");
}
}
When we run the application we can see:
--------------------------------------------
Name = John
You used as input parameter the following string: "My string".
You used as input parameter the following string: "String2".
--------------------------------------------
End of the execution
Here are some remarks:
- "lambda expressions" could be used always with a functional interface;
- the consumers could be implemented by an inner or a normal class.
- a BiConsumer is implemented as a Consumer, but it accepts 2 objects as parameters in the accept() method.
There are a couple of predefined functional interfaces:
Predicate<T>
orBiPredicates<T,U>
: used for filters, we need to define test(T) or test(T, U) method. This method returns a boolean value.Consumer<T>
orBiConsumer<T,U>
: we need to define accept(T) or accept(T, U) method. This method returns no value.Supplier<T>
: we need to return theobject . The get() method will be used to obtain the object returned by the supplier.Function<T, R>
orBiFunction<T,U,R>
: we need to define apply(T) or apply(T, U) method. This method returns an R object.UnaryOperator<T>
orBinaryOperator<T,T>
: we need to return a T object.
Info
T, U, R are generic types of objects.