Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/369.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
从Java调用scala.Function.tuple()_Java_Function_Scala_Tuples - Fatal编程技术网

从Java调用scala.Function.tuple()

从Java调用scala.Function.tuple(),java,function,scala,tuples,Java,Function,Scala,Tuples,我想用Java表达以下所有Scala代码: object TupleDemo { val tuple = (3, "Hello, world! ") /** @return str replicated x times */ def foo(x: Int, str: String) = str * x val tupledFoo1 = (foo _).tupled // partially applied function val tupledFoo2 = Functi

我想用Java表达以下所有Scala代码:

object TupleDemo {
  val tuple = (3, "Hello, world! ")

  /** @return str replicated x times */
  def foo(x: Int, str: String) = str * x

  val tupledFoo1 = (foo _).tupled // partially applied function

  val tupledFoo2 = Function.tupled(foo _) // desugared syntax for same partially applied function
}

object TupleDemoApp extends App {
  import TupleDemo._

  println(tupledFoo1(tuple)) // Hello, world! Hello, world! Hello, world!
  println(tupledFoo2(tuple)) // Hello, world! Hello, world! Hello, world!
}
这是我对Java等价物的理解:

import scala.Function1;
import scala.Function2;
import scala.Tuple2;
import scala.collection.immutable.WrappedString;
import scala.runtime.AbstractFunction2;

public class JavaTupleDemo {
   /** @return str replicated x times */
   static final Function2<Integer, String, String> foo = new AbstractFunction2<Integer, String, String>() {
       public String apply(Integer x, String str) {
           return new WrappedString(str).$times(x);
       }

       // perhaps the types for this method are incorrect?
       public Function1<Tuple2<Integer, String>, String> tupled(Tuple2 tuple2) {
           return null; // what to write here instead of null?
       }
   };

  public static void main(String[] args) {
      // works: Invoke tupled function defined in Scala from Java using Tuple2 defined in Java
      Tuple2<Object, String> tuple = new Tuple2<Object, String>(3, "Hello, World! ");
      System.out.println(TupleDemo.tupledFoo1().apply(tuple));

      // works: Invoke regular function defined in Java from Java
      System.out.println(JavaTupleDemo.foo.apply(3, "Hello, planet! "));

      // stumped: Invoke tupled function defined in Java from Java using both the Scala and the Java Tuple2 instances
  }
}
导入scala.Function1;
导入scala.Function2;
导入scala.Tuple2;
导入scala.collection.immutable.WrappedString;
导入scala.runtime.AbstractFunction2;
公共类JavaTupleDemo{
/**@return str复制了x次*/
静态最终函数2 foo=新的AbstractFunction2(){
公共字符串应用(整数x,字符串str){
返回新包装字符串(str)$次(x);
}
//可能此方法的类型不正确?
公共函数1元组(元组2元组2){
return null;//在这里写什么而不是null?
}
};
公共静态void main(字符串[]args){
//工作:使用Java中定义的Tuple2从Java中调用Scala中定义的tupled函数
Tuple2 tuple=新的Tuple2(3,“你好,世界!”);
System.out.println(TupleDemo.tupledFoo1().apply(tuple));
//works:从Java调用Java中定义的常规函数
System.out.println(JavaTupleDemo.foo.apply(3,“你好,星球!”);
//stumped:使用Scala和Java Tuple2实例从Java调用Java中定义的tuple函数
}
}

tuple
函数2
中实现(因此在
抽象函数2
中实现),因此无需在此处定义,您只需编写以下内容:

import scala.Function;
import scala.Function2;
import scala.Tuple2;
import scala.collection.immutable.WrappedString;
import scala.runtime.AbstractFunction2;

public class JavaTupleDemo {
  static final Function2<Integer, String, String> foo =
    new AbstractFunction2<Integer, String, String>() {
      public String apply(Integer x, String str) {
        return new WrappedString(str).$times(x);
      }
    };

  public static void main(String[] args) {
    Tuple2<Integer, String> tuple =
      new Tuple2<Integer, String>(3, "Hello, World! ");

    System.out.println(JavaTupleDemo.foo.tupled().apply(tuple));
    System.out.println(Function.tupled(JavaTupleDemo.foo).apply(tuple));
  }
}

也就是说,只需拆开元组,将
整数
,然后将其放回一起。

元组
Function2
中实现(因此在
AbstractFunction2
),因此无需在此处定义它,您只需编写以下代码:

import scala.Function;
import scala.Function2;
import scala.Tuple2;
import scala.collection.immutable.WrappedString;
import scala.runtime.AbstractFunction2;

public class JavaTupleDemo {
  static final Function2<Integer, String, String> foo =
    new AbstractFunction2<Integer, String, String>() {
      public String apply(Integer x, String str) {
        return new WrappedString(str).$times(x);
      }
    };

  public static void main(String[] args) {
    Tuple2<Integer, String> tuple =
      new Tuple2<Integer, String>(3, "Hello, World! ");

    System.out.println(JavaTupleDemo.foo.tupled().apply(tuple));
    System.out.println(Function.tupled(JavaTupleDemo.foo).apply(tuple));
  }
}

也就是说,只需将元组拆开,将
整数转换成
,然后将其重新组合起来。

特拉维斯·布朗的答案的问题是,他没有包含所有的代码。这是完整的代码,它没有编译

import scala.Function;
import scala.Function2;
import scala.Tuple2;
import scala.collection.immutable.WrappedString;
import scala.runtime.AbstractFunction2;

public class JavaTupleDemo {
   /** tupled() is implemented in Function2 (and therefore in AbstractFunction2)
    * @return str replicated x times */
   static final Function2<Integer, String, String> foo = new AbstractFunction2<Integer, String, String>() {
       public String apply(Integer x, String str) {
           return new WrappedString(str).$times(x);
       }
   };

  public static void main(String[] args) {
      // Invoke tupled function defined in Scala from Java using Tuple2 defined in Java
      Tuple2<Object, String> tuple = new Tuple2<Object, String>(3, "Hello, World! ");
      System.out.println(TupleDemo.tupledFoo1().apply(tuple));

      // Invoke regular function defined in Java from Java
      System.out.println(JavaTupleDemo.foo.apply(3, "Hello, planet! "));

      // Invoke tupled function defined in Java from Java using both the Scala and the Java Tuple2 instances
      System.out.println(JavaTupleDemo.foo.tupled().apply(tuple));
      System.out.println(Function.tupled(JavaTupleDemo.foo).apply(tuple));
  }
}
导入scala.Function;
导入scala.Function2;
导入scala.Tuple2;
导入scala.collection.immutable.WrappedString;
导入scala.runtime.AbstractFunction2;
公共类JavaTupleDemo{
/**tuple()是在Function2中实现的(因此也是在AbstractFunction2中实现的)
*@return str复制了x次*/
静态最终函数2 foo=新的AbstractFunction2(){
公共字符串应用(整数x,字符串str){
返回新包装字符串(str)$次(x);
}
};
公共静态void main(字符串[]args){
//使用Java中定义的Tuple2从Java调用Scala中定义的tupled函数
Tuple2 tuple=新的Tuple2(3,“你好,世界!”);
System.out.println(TupleDemo.tupledFoo1().apply(tuple));
//从Java调用Java中定义的常规函数
System.out.println(JavaTupleDemo.foo.apply(3,“你好,星球!”);
//使用Scala和Java Tuple2实例从Java调用Java中定义的tupled函数
System.out.println(JavaTupleDemo.foo.tuple().apply(tuple));
System.out.println(Function.tuple(JavaTupleDemo.foo).apply(tuple));
}
}
错误消息如下:

error: method apply in interface Function1<T1,R> cannot be applied to given types;
required: Tuple2<Integer,String>
found: Tuple2<Object,String>
reason: actual argument Tuple2<Object,String> cannot be converted to Tuple2<Integer,String> by method invocation conversion
where T1,R are type-variables:
T1 extends Object declared in interface Function1
R extends Object declared in interface Function1
错误:接口函数1中的方法apply无法应用于给定类型;
必需:Tuple2
发现:Tuple2
原因:无法通过方法调用转换将实际参数Tuple2转换为Tuple2
其中T1,R是类型变量:
T1扩展接口函数1中声明的对象
R扩展接口函数1中声明的对象
不能简单地将tuple强制转换为Tuple2。如果您尝试以下操作:

Tuple2<Object, String> scalaTuple = TupleDemo.tuple();

Tuple2<Integer, String> tuple = new Tuple2<Integer, String>(
  (Integer) scalaTuple._1(),
  scalaTuple._2()
);
System.out.println(JavaTupleDemo.foo.tupled().apply((Tuple2<Integer, String>)tuple));
System.out.println(Function.tupled(JavaTupleDemo.foo).apply((Tuple2<Integer, String>)tuple));
System.out.println(JavaTupleDemo.foo.tuple().apply((Tuple2)tuple));
System.out.println(Function.tuple(JavaTupleDemo.foo).apply((Tuple2)tuple));
错误消息是:

error: inconvertible types
required: Tuple2<Integer,String>
found:    Tuple2<Object,String>
错误:不可转换类型
必需:Tuple2
发现:Tuple2
进行此编译的唯一方法是执行一些非常糟糕的操作—首先将tuple强制转换为对象,然后再转换为Tuple2:

System.out.println(JavaTupleDemo.foo.tuple().apply((Tuple2)((Object)javaTuple));
System.out.println(Function.tuple(JavaTupleDemo.foo).apply((Tuple2)((Object)javaTuple));

有更好的方法吗?

特拉维斯·布朗的答案的问题是他没有包含所有的代码。这是完整的代码,它没有编译

import scala.Function;
import scala.Function2;
import scala.Tuple2;
import scala.collection.immutable.WrappedString;
import scala.runtime.AbstractFunction2;

public class JavaTupleDemo {
   /** tupled() is implemented in Function2 (and therefore in AbstractFunction2)
    * @return str replicated x times */
   static final Function2<Integer, String, String> foo = new AbstractFunction2<Integer, String, String>() {
       public String apply(Integer x, String str) {
           return new WrappedString(str).$times(x);
       }
   };

  public static void main(String[] args) {
      // Invoke tupled function defined in Scala from Java using Tuple2 defined in Java
      Tuple2<Object, String> tuple = new Tuple2<Object, String>(3, "Hello, World! ");
      System.out.println(TupleDemo.tupledFoo1().apply(tuple));

      // Invoke regular function defined in Java from Java
      System.out.println(JavaTupleDemo.foo.apply(3, "Hello, planet! "));

      // Invoke tupled function defined in Java from Java using both the Scala and the Java Tuple2 instances
      System.out.println(JavaTupleDemo.foo.tupled().apply(tuple));
      System.out.println(Function.tupled(JavaTupleDemo.foo).apply(tuple));
  }
}
导入scala.Function;
导入scala.Function2;
导入scala.Tuple2;
导入scala.collection.immutable.WrappedString;
导入scala.runtime.AbstractFunction2;
公共类JavaTupleDemo{
/**tuple()是在Function2中实现的(因此也是在AbstractFunction2中实现的)
*@return str复制了x次*/
静态最终函数2 foo=新的AbstractFunction2(){
公共字符串应用(整数x,字符串str){
返回新包装字符串(str)$次(x);
}
};
公共静态void main(字符串[]args){
//使用Java中定义的Tuple2从Java调用Scala中定义的tupled函数
Tuple2 tuple=新的Tuple2(3,“你好,世界!”);
System.out.println(TupleDemo.tupledFoo1().apply(tuple));
//从Java调用Java中定义的常规函数
System.out.println(JavaTupleDemo.foo.apply(3,“你好,星球!”);
//使用Scala和Java Tuple2实例从Java调用Java中定义的tupled函数
System.out.println(JavaTupleDemo.foo.tuple().apply(tuple));
System.out.println(Function.tuple(JavaTupleDemo.foo).apply(tuple));
}
}
错误消息如下:

error: method apply in interface Function1<T1,R> cannot be applied to given types;
required: Tuple2<Integer,String>
found: Tuple2<Object,String>
reason: actual argument Tuple2<Object,String> cannot be converted to Tuple2<Integer,String> by method invocation conversion
where T1,R are type-variables:
T1 extends Object declared in interface Function1
R extends Object declared in interface Function1
错误:接口函数1中的方法apply无法应用于给定类型;
必需:Tuple2
发现:Tuple2
原因:无法通过方法调用转换将实际参数Tuple2转换为Tuple2
其中T1,R是类型变量:
T1扩展接口函数1中声明的对象
R扩展接口函数1中声明的对象
不能简单地将tuple强制转换为Tuple2。如果您尝试以下操作:

Tuple2<Object, String> scalaTuple = TupleDemo.tuple();

Tuple2<Integer, String> tuple = new Tuple2<Integer, String>(
  (Integer) scalaTuple._1(),
  scalaTuple._2()
);
System.out.println(JavaTupleDemo.foo.tupled().apply((Tuple2<Integer, String>)tuple));
System.out.println(Function.tupled(JavaTupleDemo.foo).apply((Tuple2<Integer, String>)tuple));
System.out.println(JavaTupleDemo.foo.tuple().apply((Tup