Home > OS >  return object with lambda in java
return object with lambda in java

Time:05-28

how do I return an object using lambda in java? I try using objeto = (Runnable[] obj, int pos) -> {obj[pos ]}; in main() but not work.

is it possible to return object obj[pos] using lambda inside inst() function?

import java.util.Comparator;

public class Runna{
    public static Runnable inst(Runnable[] obj, int pos){
        return obj[pos];
    }
}

CodePudding user response:

It sounds like you want to do this.

Runnable[] array = new Runnable[10]; // some array filled with Runnables.
BiFunction<Runnable[],  Integer, Runnable> lookup = (arr, i) -> arr[i];
Runnable foo = lookup.apply(array, 3);

Now you can do foo.run();

You needed a BiFunction to accept two disparate arguments and return a third.

  • Related