Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass Consumer<String> to the method

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?

like image 243
Suule Avatar asked Feb 08 '19 08:02

Suule


People also ask

How do you pass a Consumer to a method in Java?

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.

What is Consumer <> in Java?

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.

What is method name of Consumer interface?

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.

What is a lambda Consumer?

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().


2 Answers

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.

like image 124
Ravindra Ranwala Avatar answered Oct 28 '22 07:10

Ravindra Ranwala


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 -> {});
like image 25
Naman Avatar answered Oct 28 '22 06:10

Naman