Scala - > Java,Pass函数作为另一个函数的参数

时间:2015-11-16 16:23:07

标签: java scala

我正在探索scala和Java 1.8,但无法在java 1.8 lambda表达式中找到相应的代码。

Scala代码:

object Ex1 extends App {

  def single(x:Int):Int =x
  def square(x:Int):Int = x * x
  def cube(x:Int):Int = x*x*x

  def sum(f:Int=>Int,a:Int,b:Int):Int=if (a>b) 0 else f(a) + sum(f,a+1,b)

  def sumOfIntegers(a:Int,b:Int)=sum(single,a,b)
  def sumOfSquares(a:Int,b:Int)=sum(square,a,b);
  def sumOfCubes(a:Int,b:Int)=sum(cube,a,b);

  println(sumOfIntegers(1,4));
  println(sumOfSquares(1,4));
  println(sumOfCubes(1,4));

}

output:

10
30
100

的java:

public class Test1{
    public int single(int x){
        return x;
    }
    public int square(int x){
        return x * x;
    }
    public int cube(int x){
        return x * x * x;
    }
    // what's next? How to implement sum() method as shown in Scala?
    // Stuck in definition of this method, Stirng s should be function type.
    public int sum( Sring s , int a, int b){
        // what will go here?
    }
    public int sumOfIntegers(int a, int b){
        return sum("single<Function> how to pass?",a,b);
    }
    public int sumOfSquares(int a, int b){
        return sum("square<Function> how to pass?",a,b);
    }
    public int sumOfCubes(int a, int b){
        return sum("cube<Function> how to pass?",a.b);
    }
}

是否可以通过JDK 1.8实现相同的目标?

4 个答案:

答案 0 :(得分:1)

是的,有可能:

 public Integer sumSingle(Integer a) { return a; }

 public int sum(Function<Integer, Integer> f, int a, int b)
 {
      ... : f.apply(a+1); // or whatever
 }

 public int sumOfIntegers(int a, int b)
 {
     return sum(Test1::single, a, b);
 }

构造Class::method从方法创建一个Function对象。或者,如果您不需要重复使用它,则可以直接传递参数:(Integer a) -> a*a* ...

答案 1 :(得分:1)

Function<Integer, Integer> single = x -> x;
Function<Integer, Integer> square = x -> x*x;

public int sum( Function<Integer, Integer> s , int a, int b){
    if (a>b){
        return 0;
    } else {
        s.apply(a) + sum(s,a+1,b)
    }
}

答案 2 :(得分:1)

您将需要定义要使用的方法。 Java中唯一附带的是Function<X,Y> Integersingle)可用于squarecubeBiFunction<T,Y, R>以及sumOf可以用于三个interface Sum { Integer apply(Function<Integer, Integer> func, int start, int end); }

cube

单个,正方形和立方体可以是类中的任一个方法(请参阅Fuction或内联(请参阅其他两个)。它们是variableName.apply。然后可以将此函数传递给其他方法并使用 class Test { private static Integer sum(Function<Integer, Integer> func, int a, int b) { if (a>b) return 0; else return func.apply(a) + sum(func,a+1,b); } private static Integer cube(Integer a) { return a * a * a; } public static void main (String[] args) throws java.lang.Exception { Function<Integer,Integer> single = a -> a; Function<Integer,Integer> square = a -> a * a; Function<Integer,Integer> cube = Test::cube; // You can not do the sum in-line without pain due to its recursive nature. Sum sum = Test::sum; BiFunction<Integer, Integer, Integer> sumOfIntegers = (a, b) -> sum.apply(single, a, b); BiFunction<Integer, Integer, Integer> sumOfSquares = (a, b) -> sum(square, a, b); // You could just use the static method directly. BiFunction<Integer, Integer, Integer> sumOfCubes = (a, b) -> sum(cube, a, b); System.out.println(sumOfIntegers.apply(1,4)); System.out.println(sumOfSquares.apply(1,4)); System.out.println(sumOfCubes.apply(1,4)); } } 进行调用。

package chatting

import javax.websocket.server.ServerEndpoint;
import javax.websocket.OnMessage;

@ServerEndpoint("/echo")
public class WebsocketHomeController {

    def index() { }

    @OnMessage


    public String echo(String incomingMessage) {
            return "I got this (" + incomingMessage + ")"
            + " so I am sending it back !";
        }
    }

答案 3 :(得分:1)

在Java 8中,您可以使用任何functional interface,即只有一个非静态方法的接口,而不使用默认实现,而不是Scala的Function<N>类型。 java.util.function包中有很多这样的接口,用于单参数和双参数函数;特别是对于获取和返回int的函数,您应该使用IntUnaryOperator,这已在其他人的评论中提到过。如果您有两个以上的参数或两个不同类型的参数,请使用一个方法定义您自己的接口。例如。对于3个参数的函数,这些参数采用intlonglong并返回非基元:

interface Fun3ILLO<T> {
    public T apply(int arg1, long arg2, long arg3);
}

(显然你可以选择你想要的界面和方法名称。)