-5

I'm trying to write a method in java that passes in another method as a parameter.

This is the method call in main:

    Functions.print("total",Functions.compute(scoreArray),Functions.find(scoreArray));

So far i have this as the method signature:

    public void print(String str, ...)

I have the compute and find methods already written. I don't know how to input them as parameters.

ljgw
  • 2,741
  • 1
  • 19
  • 39

3 Answers3

0

You can't currently do it with Java (well, you can but it's really roundabout)

There's a guava library that makes it easier to do functional programming with collections: https://code.google.com/p/guava-libraries/wiki/FunctionalExplained

tom
  • 2,646
  • 14
  • 28
0

Yes, but not directly. Create an interface:

public interface MyFuncFromAToB{
    public SomeClass calculate(SomeOtherClass in);
}

and accept that.

On Java 8 you'll be able to use Lambdas. When that comes around keep accepting the same interface(it must have only one method) and you'll receive lambdas and method pointers with that signature.

nanofarad
  • 38,481
  • 4
  • 83
  • 110
0

Java and javascript are two very different languages. Java (currently) doesn't have any functional programming features; methods (functions) are not Objects and so cannot be passed around like objects.

trf
  • 1,224
  • 13
  • 30