相关疑难解决方法(0)

Thread.sleep的方法引用不明确

我遇到一个奇怪的问题,方法引用Thread::sleep是模糊的,但具有相同签名的方法不是.

package test;    

public class Test
{
    public static void main(String[] args)
    {
        foo(Test::sleep, 1000L); //fine
        foo((FooVoid<Long>)Thread::sleep, 1000L); //fine
        foo(Thread::sleep, 1000L); //error
    }

    public static void sleep(long millis) throws InterruptedException
    {
        Thread.sleep(millis);
    }

    public static <P, R> void foo(Foo<P, R> function, P param) {}

    public static <P> void foo(FooVoid<P> function, P param) {}

    @FunctionalInterface
    public interface Foo<P, R> {
        R call(P param1) throws Exception;
    }

    @FunctionalInterface
    public interface FooVoid<P> {
        void call(P param1) throws Exception;
    }
}
Run Code Online (Sandbox Code Playgroud)

我得到了这两个错误: …

java java-8 functional-interface

16
推荐指数
3
解决办法
655
查看次数

为什么将此Java方法调用视为模棱两可?

我遇到了奇怪的错误消息,我认为这可能是不正确的。考虑以下代码:

public class Overloaded {
    public interface Supplier {
        int get();
    }

    public interface Processor {
        String process(String s);
    }

    public static void load(Supplier s) {}
    public static void load(Processor p) {}

    public static int genuinelyAmbiguous() { return 4; }
    public static String genuinelyAmbiguous(String s) { return "string"; }

    public static int notAmbiguous() { return 4; }
    public static String notAmbiguous(int x, int y) { return "string"; }

    public static int strangelyAmbiguous() { return 4; }
    public static String strangelyAmbiguous(int …
Run Code Online (Sandbox Code Playgroud)

java overloading arity functional-interface method-reference

15
推荐指数
1
解决办法
377
查看次数

Java编译错误:结合重载的方法引用

我有以下类与重载方法:

import java.util.ArrayList;
import java.util.concurrent.Callable;

public abstract class Test {

  public void test1 () {
    doStuff (ArrayList::new); // compilation error
  }

  public void test2 () {
    doStuff ( () -> new ArrayList<> ());
  }

  public abstract void doStuff (Runnable runable);

  public abstract void doStuff (Callable<ArrayList<String>> callable);
}
Run Code Online (Sandbox Code Playgroud)

该方法test1导致错误消息的编译错误 The method doStuff(Runnable) is ambiguous for the type Test.

我添加了第三种方法test3,如下所示:

public void test3 () {
    doStuff ( () -> {
      new ArrayList<> ();
    });
  }
Run Code Online (Sandbox Code Playgroud)

这里doStuff(Runnable) …

java overloading compiler-errors java-8 method-reference

11
推荐指数
1
解决办法
238
查看次数

如何解决Java泛型中交集类型导致的模糊方法?

我刚刚发现您可以在单个类型参数绑定中指定多个类型(请参阅示例).像任何新工具一样,我一直在尝试探索如何使用(和误用)的可能性.我精心设计了这个例子以帮助说明.

在下面的示例中,编译器给出了一个错误

dispatch(new AlphabetSoup());

对于Demo类型,方法dispatch(Demo.Soup)是不明确的

我能理解这一点,因为任何方法签名都匹配.我的问题是如何在不改变方法的情况下解决这个问题?如果我想要强制调用Soup版本,我可以将其转发给Soup:

dispatch((Soup)new AlphabetSoup())

但我不确定你是如何强制拨打其他版本的.可能吗?

public class Demo {

    interface HasA { public char getA(); }
    interface HasB { public char getB(); }
    interface HasC { public char getC(); }

    interface Soup { 
        public void eat();
    }

    class Alphabet implements HasA, HasB, HasC {
        public char getA() { return 'a'; }
        public char getB() { return 'b'; }
        public char getC() { return 'c'; }
    }

    class AlphabetSoup implements Soup,  HasA, HasB, HasC  { 
        public void …
Run Code Online (Sandbox Code Playgroud)

java generics

9
推荐指数
4
解决办法
2万
查看次数

Maven和apache utils的模糊编译错误

我使用org.apache.commons.lang3.BooleanUtilscommons-lang3(3.1版本).当我尝试编译下一行代码时

BooleanUtils.xor(true, true);
Run Code Online (Sandbox Code Playgroud)

使用maven-compiler-plugin(版本3.3),我收到编译失败消息:

[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.3:compile (default-compile) on project exchange: Compilation failure
[ERROR] MyClass.java:[33,34] reference to xor is ambiguous, both method xor(boolean...) in org.apache.commons.lang3.BooleanUtils and method xor(java.lang.Boolean...) in org.apache.commons.lang3.BooleanUtils match
Run Code Online (Sandbox Code Playgroud)

我使用Java 1.7.0_55进行编译.

我怎么解决这个问题?

java apache apache-commons maven apache-commons-lang3

9
推荐指数
1
解决办法
1911
查看次数