简体   繁体   中英

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")
    }
});

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"

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM