How to refactor a lambda expression to a method reference?

slartidan :

My code looks like this:

class A {
    public void m() {
        Arrays.stream("a", "b")
            .map(x -> x + "!") // <-- lambda expression
            .forEachOrdered(System.out::println);
    }
}

I want to refactor it into:

class A {
    public void m() {
        Arrays.stream("a", "b")
            .map(this::t) // <-- method reference
            .forEachOrdered(System.out::println);
    }

    public String t(String x) {
        return x + "!";
    }
}

How can I do this refactoring with IntelliJ?

Andy Turner :

Select the lambda, press Alt+Enter, click "Extract to Method Reference".

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=470653&siteId=1