so I do have code like this:
public ConsumerTestClass(Consumer<String> consumer) {
}
public static void printString(String text) {
System.out.println(text);
}
And from the method of other class, I would like to create object of ConsumerTestClass
:
new ConsumerTestClass(/*passing consumer here*/);
And as a consumer I would like to pass ConsumerTestClass::printString
, but to be able to do that I need to pass argument as well, so it looks like that: (text) -> ConsumerTestClass.printString(text)
. And my question is... Is it only option to pass Consumer, or method which is accepting one argument and returning no results?
java. util. function. Consumer<T> Consumer function type Parameters: T - object type to be passed to the Consumer accept method Consumer function methods: void accept(T t) This method operates on a single object passed in as an argument.
Java Consumer is a functional interface which represents an operation that accepts a single input argument and returns no result. Unlike most other functional interfaces, Consumer is expected to operate via side-effects.
accept() method is the primary abstract method of the Consumer functional interface. Its function descriptor being T -> () . I.e. accept() method takes as input the type T and returns no value.
A Consumer interface is a predefined functional interface that can be used when creating lambda expressions or method references. This interface represents an operation that accepts a single input parameter and doesn't return anything. It contains only one method named accept().
The method reference ConsumerTestClass::printString
is just a syntactic sugar to the equivalent lambda expression text -> ConsumerTestClass.printString(text)
A method reference can't be used for any method. They can only be used to replace a single-method lambda expression. In general, we don't have to pass paremeters to method references. In this case, parameters taken by the method printString
are passed automatically behind the scene.
method references provides a way to generate function objects even more succinct than lambdas.
So prefer method references to lambdas as a good practice.
Here's the fully working example.
public class ConsumerTestClass {
public ConsumerTestClass(Consumer<String> consumer) {
consumer.accept("Test");
}
public static void printString(String text) {
System.out.println(text);
}
public static void main(String[] args) {
new ConsumerTestClass(ConsumerTestClass::printString);
}
}
The bottom line is that you have to pass in the argument when you call the accept
method of the Consumer
as done inside the constructor above.
You can pass it as :
new ConsumerTestClass(ConsumerTestClass::printString);
which is a method reference for
new ConsumerTestClass(x -> ConsumerTestClass.printString(x));
Is it only option to pass Consumer, or method which is accepting one argument and returning no results?
It depends on your use case, for example, if you want to pass two arguments and still not return anything from them, you can look into the BiConsumer
class as well.
Note: A special case, when you don't want to perform any operation on the Consumer
supplied, you can chose to define it as no-op lambda, as in:
new ConsumerTestClass(s -> {});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With