Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Replace lambda expression with equivalent code in IntelliJ IDEA

Can I replace a lambda expression with an equivalent expression using IntelliJ IDEA refactoring. Thanks.

public class StateOwner {
    public void addStateListener(StateChangeListener listener) { ... }
}

..
StateOwner stateOwner = new StateOwner();

stateOwner.addStateListener(
    (oldState, newState) -> System.out.println("State changed")
);

Can I have IntelliJ IDEA replace the lambda expression part with

stateOwner.addStateListener(new StateChangeListener() {

    public void onStateChange(State oldState, State newState) {
        System.out.println("State changed")
    }
});
like image 801
user674669 Avatar asked Mar 24 '17 04:03

user674669


2 Answers

It depends on your hot-key setting. But if you use default, you can try lead cursor on lambda narrow or lamdba arguments, then alt + Enter, then choose "replace lambda with anonymous class"

like image 173
Oleg Zinoviev Avatar answered Nov 14 '22 21:11

Oleg Zinoviev


It offers that in the same way as it offers to turn your code into a lambda. Make sure you have a fairly late version.

like image 45
Just another Java programmer Avatar answered Nov 14 '22 22:11

Just another Java programmer