0

Is it possible to pass a reference to a method as a parameter?

For example:

public static void main(String[] args) {
    String data = "name: John, random text, address: leetStreet";
    Person person;

    //if regex matches, use method reference, to send the result. 
    applyDataToPerson(data, "name: (\\w+)", person, Person::setName);
    applyDataToPerson(data, "address: (\\w+)", person, Person::setAddress);
}

private static void applyDataToPerson(String data, String regex, Person person, 
 Function<Person> f) {
    Matcher match = Pattern.compile(regex).matcher(data);
    if (match.matches()) person.f(match.group(1));
}

class Person {
    private String name;
    private String address;

    public void setName(String name) {
        this.name = name;
    }

    public void setAddress(String address) {
        this.address = address;
    }
}

If not, what would be an alternative to giving a reference to a method? Switch-case construct?

3
  • 2
    In Java 8 you can pass method references to existing methods (among other things) Commented Oct 27, 2016 at 21:44
  • @Tibrogargan how would the method signature look like? Comparator <? super T> does not make sense to me. Commented Oct 27, 2016 at 21:51
  • 1
    What is Function<Person> supposed to represent? If we're talking about java.util.function.Function, it has 2 generic types, not 1. So that code cannot compile to begin with... (let alone person.f(...)). Before asking for alternatives, make sure you have working code... Commented Oct 27, 2016 at 22:00

1 Answer 1

5

I think you're looking for BiConsumer:

private static void applyDataToPerson(String data, String regex, Person person, BiConsumer<Person, String> action) {
    Matcher match = Pattern.compile(regex).matcher(data);
    if (match.matches()) action.accept(person, match.group(1));
}

Alternatively, you can shorten the method signature and use a single-argument Consumer that captures your person reference:

public static void main(String[] args) {
    String data = "name: John, random text, address: leetStreet";
    Person person;

    //if regex matches, use method reference, to send the result. 
    applyData(data, "name: (\\w+)", person::setName);
    applyData(data, "address: (\\w+)", person::setAddress);
}

private static void applyData(String data, String regex, Consumer<String> action) {
    Matcher match = Pattern.compile(regex).matcher(data);
    if (match.matches()) action.accept(match.group(1));
}
Sign up to request clarification or add additional context in comments.

3 Comments

And in your main method write the biconsumer function : applyDataToPerson(data, "name: (\\w+)", person, (Person p, String s) -> p.setName(s) ); applyDataToPerson(data, "address: (\\w+)", person, (Person p,String s) -> p.setAddress(s));
@gaston No, the main method is exactly as OP had it.
Personally I like the second option.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.