Generics Java8-功能接口、Lambda和方法参考

Generics Java8-功能接口、Lambda和方法参考,generics,lambda,java-8,method-reference,functional-interface,Generics,Lambda,Java 8,Method Reference,Functional Interface,我有以下代码 public class FunctionalInterfaceTest { @FunctionalInterface public interface FunctionThatThrows<T, R> { R apply(T t) throws Exception; } public void perform() { try { unchecked((String x) -> Integer.parseInt

我有以下代码

   public class FunctionalInterfaceTest {

  @FunctionalInterface
  public interface FunctionThatThrows<T, R> {
    R apply(T t) throws Exception;
  }

  public void perform() {
    try {
      unchecked((String x) -> Integer.parseInt(x)).apply("abc");
    } catch (Exception e) {
      System.out.println("Encountered exception" + e.getMessage());
    }
  }

  public void perform1() {
    try {
      unchecked(<fill-in-here-using-method-reference>);
    } catch (Exception e) {
      System.out.println("Encountered Exception" + e.getMessage());
    }
  }

  public <T, R> Function<T, R> unchecked(FunctionThatThrows<T, R> f) {
    return (a) -> {
      try {
        return f.apply(a);
      } catch (Exception e) {
        throw new RuntimeException(e);
      }
    };
  }

  public static void main(String[] args) {
    FunctionalInterfaceTest test = new FunctionalInterfaceTest();
    test.perform();
    test.perform1();
  }
}
公共类功能接口测试{
@功能接口
抛出的公共接口函数{
R apply(T)抛出异常;
}
公开执行{
试一试{
未选中((字符串x)->Integer.parseInt(x)).apply(“abc”);
}捕获(例外e){
System.out.println(“遇到异常”+e.getMessage());
}
}
公共图书馆1(){
试一试{
未检查();
}捕获(例外e){
System.out.println(“遇到异常”+e.getMessage());
}
}
未选中公共函数(函数f){
返回(a)->{
试一试{
报税表f.apply(a);
}捕获(例外e){
抛出新的运行时异常(e);
}
};
}
公共静态void main(字符串[]args){
FunctionInterfaceTest测试=新的FunctionInterfaceTest();
test.perform();
test.perform1();
}
}
我想在perform1方法中使用方法引用,以获得与perform方法类似的结果。我尝试在perform1中使用Integer::parseInt之类的方法引用,但它不起作用。如何对方法引用执行相同的操作?为什么方法引用会给出错误

如何对方法引用执行相同的操作?为什么方法引用会给出错误

不能,除非存在显式类型。由于未选中的
方法使用泛型参数
函数抛出
,编译器不知道这些泛型类型
t
R
是什么。以下内容将不会编译:

unchecked(s -> Integer.parseInt(s));    // doesn't compile
编译器认为
s
是与方法不兼容的
对象。这就是为什么它是模棱两可的

关键是,必须明确说明泛型参数的类型。有三种方法可以定义类型并保留泛型解决方案

  • 如上所述,使用向下转换指定
    s
    String

    unchecked((String x) -> Integer.parseInt(x))                 // no method reference
    
  • 使用传递的lambda表达式的显式强制转换。这个解决方案是IntelliJ Idea提供给我的第一个解决方案。现在很清楚,编译器输入和输出传递的函数分别是
    String
    Integer
    。请注意,此结构需要`:

    unchecked((FunctionThatThrows<String, Integer>) Integer::parseInt); // with method reference
    
    unchecked((functionThrows)Integer::parseInt);//方法参考
    
  • 明确定义所需的返回类型:

    Function<String, Integer> f = unchecked((FunctionThatThrows<String, Integer>) Integer::parseInt);
    
    Function f=unchecked((functionThrows)Integer::parseInt);
    
    。。应省略多余的铸件,并给出更清晰的结果:

    Function<String, Integer> f = unchecked(Integer::parseInt);  // with method reference
    
    函数f=unchecked(整数::parseInt);//方法参考
    
    (注:这一条也被纳入了报告)


  • 您是否尝试过取消选中((FunctionThrows)Integer::parseInt)。应用(“abc”)
    ?哪种方式都不行?@Naman,行得通。为什么需要显式类型转换?有多少
    parseInt
    可能与您的
    Integer::parseInt
    匹配?当您强制转换时,您明确地告诉编译器要使用哪个
    parseInt
    ,我认为非常明确的是,我们使用的是java.lang.Integer中的parseInt。为什么会有歧义?我不知道为什么编译器没有选择正确的方法,但这是一个实际相关的问题吗?我的意思是,在现实生活中,你会使用结果,比如
    函数f=unchecked(Integer::parseInt)。。。