Return value by lambda in Java

久未见 提交于 2020-05-25 12:25:20

问题


Till now I manage to find all answers I need but this one confusing me. Let's say we have example code:

public class Animal {
   private String species;
   private boolean canHop;
   private boolean canSwim;
   public Animal(String speciesName, boolean hopper, boolean swimmer) {
     species = speciesName;
     canHop = hopper;
     canSwim = swimmer;
   }
  public boolean canHop() { return canHop; }
  public boolean canSwim() { return canSwim; }
  public String toString() { return species; }
}

public interface CheckAnimal {
   public boolean test(Animal a);
}

public class FindSameAnimals {
   private static void print(Animal animal, CheckAnimal trait) {
      if(trait.test(animal)){
         System.out.println(animal);
      }

   public static void main(String[] args) {
      print(new Animal("fish", false, true), a -> a.canHop());
   }
}

OCA Study Guide (Exam 1Z0-808) book says that these two lines are equivalent:

a -> a.canHop()
(Animal a) -> { return a.canHop(); }

Does this mean that, behind the scenes, Java adds keyword return to code in the first case?

If answer is YES then how next code compile (imagine everything else is in proper place):

static int counter = 0;
ExecutorService service = Executors.newSingleThreadExecutor();
service.execute(() -> counter++));

if we know that signatures for execute and Runnable's run are:

void execute(Runnable command)
void run()

If answer is NO then how Java know when it need to return something and when not to? Maybe in

a -> a.canHop()

case we wanted to ignore boolean return type of method.


回答1:


Does this mean that, behind the scenes, Java adds keyword return to code in the first case?

No, The compiler generates byte code, and it might generate the same byte code but it doesn't change the syntax and then compile it again.

we wanted to ignore boolean return type of method.

It has the option of ignoring a value based on what functional interfaces it is considering.

a -> a.canHop()

could be

(Animal a) -> { return a.canHop(); }

or

(Animal a) -> { a.canHop(); }

based on context, however it favours the first if possible.

Consider ExecutorService.submit(Callable<T>) and ExecutorService.submit(Runnable)

ExecutorService es = Executors.newSingleThreadExecutor();
es.execute(() -> counter++); // has to be Runnable
es.submit(() -> counter++); // Callable<Integer> or Runnable?

Saving the return type you can see it's a Callable<Integer>

final Future<Integer> submit = es.submit(() -> counter++);

To try yourself, here is a longer example.

static int counter = 0;

public static void main(String[] args) throws ExecutionException, InterruptedException {
    ExecutorService es = Executors.newSingleThreadExecutor();

    // execute only takes Runnable
    es.execute(() -> counter++);

    // force the lambda to be Runnable
    final Future<?> submit = es.submit((Runnable) () -> counter++);
    System.out.println(submit.get());

    // returns a value so it's a Callable<Integer>
    final Future<Integer> submit2 = es.submit(() -> counter++);
    System.out.println(submit2.get());

    // returns nothing so it must be Runnable
    final Future<?> submit3 = es.submit(() -> System.out.println("counter: " + counter));
    System.out.println(submit3.get());

    es.shutdown();
}

prints

null
2
counter: 3
null

The first submit take a Runnable so Future.get() returns null

The second submit defaults to being a Callable so Future.get() returns 2

The third submit can only be a void return value so it must be a Runnable so Future.get() returns null




回答2:


Yes, when specifying only a single statement, its value is automatically returned from the lambda.

Then, since Runnable is a Functional Interface, it can be defined as a lambda. The return type is void, so any return value inside the lambda will be ignored.




回答3:


You are confused about the scope of the return statement. The return statement (whether inserted as bytecode by the compiler or as source code by the programmer) returns from the lambda, and not from the method that calls the lambda.

void foo() {
    Supplier<String> s = () -> { return "bar" };
    String s = s.get(); // s is assigned to "bar"
    // Execution continues as the return statement in the lambda only returns from the lambda and not the enclosing method
    System.out.println("This will print");
}


来源:https://stackoverflow.com/questions/38540481/return-value-by-lambda-in-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!