-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,751
  • 1
  • 20
  • 39
AbbyD
  • 51
  • 6

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,704
  • 16
  • 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
  • 40,330
  • 4
  • 86
  • 117
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,279
  • 1
  • 13
  • 33