Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/339.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在Scala中使用Java lambdas_Java_Scala_Lambda_Scala Java Interop - Fatal编程技术网

如何在Scala中使用Java lambdas

如何在Scala中使用Java lambdas,java,scala,lambda,scala-java-interop,Java,Scala,Lambda,Scala Java Interop,我有以下代码: source .mapValues(value -> value + " Stream it!!!") .print(Printed.toSysOut()); 如您所见,mapValues需要一个lambda表达式 现在,我正在使用Java库,但应用程序是用Scala编写的。如何将Scala lambda传递给Java代码 我尝试了以下方法: source .mapValues(value => value + "hello") .print

我有以下代码:

source
    .mapValues(value -> value + " Stream it!!!")
    .print(Printed.toSysOut());
如您所见,
mapValues
需要一个lambda表达式

现在,我正在使用Java库,但应用程序是用Scala编写的。如何将Scala lambda传递给Java代码

我尝试了以下方法:

source
  .mapValues(value => value + "hello")
  .print(Printed.toSysOut)
但编译器抱怨:

[error]   (x$1: org.apache.kafka.streams.kstream.Printed[String,?0(in value x$1)])Unit <and>
[error]   (x$1: org.apache.kafka.streams.kstream.KeyValueMapper[_ >: String, _ >: ?0(in value x$1), String])Unit <and>
[error]   (x$1: String)Unit
[error]  cannot be applied to (org.apache.kafka.streams.kstream.Printed[Nothing,Nothing])
[error]       .print(Printed.toSysOut)
[error]        ^
[error] two errors found
[error] (compile:compileIncremental) Compilation failed
[error] Total time: 2 s, completed Nov 19, 2017 7:53:44 PM
[error](x$1:org.apache.kafka.streams.kstream.Printed[String,?0(值x$1)]单位
[错误](x$1:org.apache.kafka.streams.kstream.KeyValueMapper[\u>:String,\u>:?0(值x$1),String])单位
[错误](x$1:字符串)单位
[错误]无法应用于(org.apache.kafka.streams.kstream.Printed[Nothing,Nothing])
[错误].打印(打印的.toSysOut)
[错误]^
[错误]发现两个错误
[错误](编译:编译增量)编译失败
[错误]总时间:2秒,已完成2017年11月19日下午7:53:44

错误消息列出了
print
支持的参数类型。其中之一是:

org.apache.kafka.streams.kstream.Printed[String,?0(in value x$1)]
从错误消息中,您可以看到您正在为
Printed.toSysOut
提供一种类型的:

org.apache.kafka.streams.kstream.Printed[Nothing,Nothing]
Printed
未出现在卡夫卡1.1中),
toSysOut
定义为:

public static <K,V> Printed<K,V> toSysOut()

这取决于您的Scala版本

在2.12中,Scala函数可用于预期使用Java函数的地方,反之亦然

App1.java

在2.11中,您可以使用
scala-java8-compat

libraryDependencies += "org.scala-lang.modules" %% "scala-java8-compat" % "0.8.0"
App1.java

或者,在Scala的2.11中,您可以在
java.util.function.function
Scala.Function1
之间切换

所以如果你用2.11试试看

source
  .mapValues((value => value + "hello").asJava)
  .print(Printed.toSysOut) 


在2.11中,这一行可能不会编译:
.mapValues(value=>value+“hello”)
。我已经像你说的那样尝试了,但是编译器仍然说
[error](x$1:org.apache.kafka.streams.kstream.Printed[String,?0(In value x$1)]Unit[error](x$1:org.apache.kafka.streams.kstream.KeyValueMapper[\u>:String,\u>:0(In value x$1),String])Unit[error](x$1:String)Unit[error]无法应用于(org.apache.kafka.streams.kstream.Printed[String,String])[error]。print(Printed.toSysOut[String,String])[error]^[error]发现一个错误[error](编译:compileIncremental)编译失败
Scala在调用
mapValues
后可能无法推断
KStream
类型。你能试试像
.mapValues[String](…)
这样的方法来帮助Scala编译器计算出类型吗?你帮我省了一天。非常感谢。通常Scala比Java对类型更严格,所以在调用Java函数时,您经常需要指定泛型类型参数。
object App {
  def main(args: Array[String]): Unit = {
    App1.method((s: String) => s.toUpperCase)
  }

  def method1(function: String => String): Unit = {
    println(function("xyz"))
  }
}
libraryDependencies += "org.scala-lang.modules" %% "scala-java8-compat" % "0.8.0"
import java.util.function.Function;
import static scala.compat.java8.JFunction.func;

public class App1 {
    public static void method(Function<String, String> function) {
        System.out.println(function.apply("a"));
    }

    public static void main(String[] args) {
        App.method1(func((String s) -> s.toUpperCase()));
    }
}
import scala.compat.java8.FunctionConverters._

object App {
  def main(args: Array[String]): Unit = {
    App1.method(((s: String) => s.toUpperCase).asJava)
  }

  def method1(function: String => String): Unit = {
    println(function("xyz"))
  }
}
source
  .mapValues((value => value + "hello").asJava)
  .print(Printed.toSysOut) 
source
  .mapValues(((value: String) => value + "hello").asJava)
  .print(Printed.toSysOut[String, String])