Java 8基于Lambda表达式的带返回值方法引用技术问询
Hey there! Let's break this down clearly—you're trying to pass a method (let's call it method1) into another method (method2) so method2 can execute method1 and use its return value. This is exactly what Java 8's functional interfaces, lambdas, and method references were designed for!
First, the core idea: Java doesn't let you pass methods directly, but you can wrap a method's behavior with a functional interface. Lambdas and method references are essentially instances of these interfaces—this is the key to making your work flow.
Step 1: Pick the right functional interface
Your use case involves a method that takes no arguments and returns a boolean. Java 8 has ready-made interfaces for this:
Supplier<Boolean>: A general-purpose interface for no-arg return values (callget()to get the result)BooleanSupplier: A specialized interface for boolean returns (callgetAsBoolean()—this is more readable for your scenario, so it's recommended)
Step 2: Define your method1 (example)
Let's say you have a method that checks some condition:
public class MyClass { // Instance method example public boolean checkCondition() { // Your business logic here that returns true/false return System.currentTimeMillis() % 2 == 0; } // Static method example (if your method is static) public static boolean staticCheckCondition() { return Math.random() > 0.5; } }
Step 3: Define method2 to accept the method reference
Method2 needs to take a functional interface as a parameter, then call it and handle the return value:
public void processMethod(BooleanSupplier conditionChecker) { // Call the passed-in method via the functional interface boolean result = conditionChecker.getAsBoolean(); // This matches your pseudocode's if-check logic if (result) { System.out.println("Method1 returned true—run corresponding logic"); } else { System.out.println("Method1 returned false—run other logic"); } }
Step 4: Pass method1 to method2 via method reference or lambda
Now you can pass method1 to method2 using either a method reference (cleaner) or a lambda:
Case 1: Pass an instance method
MyClass myObj = new MyClass(); // Use method reference for the instance method processMethod(myObj::checkCondition); // Equivalent lambda (if you prefer that style) processMethod(() -> myObj.checkCondition());
Case 2: Pass a static method
// Use method reference for the static method processMethod(MyClass::staticCheckCondition); // Equivalent lambda processMethod(() -> MyClass.staticCheckCondition());
Why your pseudocode didn't work directly
Your line if (function.run() == true was missing a key piece: function needs to be an instance of a functional interface, not a raw "method". By wrapping the method in a functional interface, you turn its behavior into a passable object that method2 can call and get a return value from.
Optional: Define your own functional interface
If Java's built-in interfaces don't fit your needs (e.g., if you need parameters later), you can make your own:
@FunctionalInterface public interface MyChecker { boolean run(); // Matches the `run()` in your pseudocode }
Then adjust method2 to use your custom interface, which aligns exactly with your original pseudocode:
public void processMethod(MyChecker function) { if (function.run()) { // This is almost identical to your pseudocode! // Handle true case } else { // Handle false case } } // Call it with a method reference processMethod(myObj::checkCondition);
内容的提问来源于stack exchange,提问作者Marcus Persson




