无法推断函数接口类型错误在Java中。

huangapple go评论73阅读模式
英文:

Cannot infer functional interface type Error in Java

问题

我使用Java来实现桶排序。我想要对输入数组 [0.897, 0.565, 0.656, 0.1234, 0.665, 0.3434] 进行排序,并创建了一个包含 List<Double> 元素的 buckets 数组,我分别使用 List.sort 对每个 List<Double> 进行排序,然后将它们连接起来得到结果。

但是,当我使用 ArrayList.sort 方法对 List 进行排序时出现了错误。我将 Lambda 表达式作为 sort 函数的参数,并从IDE中得到了一个错误消息,它说 无法推断函数接口类型

错误出现在这一行:

buckets[i].sort((double a, double b) -> (int)Math.signum(a-b));

但当我将它更改为

buckets[i].sort((a, b) -> (int)Math.signum(a-b));

就没有错误,并且代码正常工作。

我非常困惑为什么它无法推断?提前感谢您。

完整的代码如下:

import java.util.ArrayList;
import java.util.List;

class Solution {
    void bucketSort(double[] arr, int n) {
        // 创建桶
        List<Double>[] buckets = new ArrayList[n];
        for (int i = 0; i < n; ++i) {
            buckets[i] = new ArrayList<Double>();
        }

        // 将输入添加到桶中
        for (int i = 0; i < n; ++i) {
            int index = (int) (arr[i] * 10);
            buckets[index].add(arr[i]);
        }

        // 分别对每个列表排序
        for (int i = 0; i < n; ++i) {
            buckets[i].sort((a, b) -> (int) Math.signum(a - b));
        }

        // 连接桶中的元素
        int index = 0;
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < buckets[i].size(); j++) {
                arr[index] = buckets[i].get(j);
                index++;
            }
        }
    }

    public static void main(String args[]) {
        double[] arr = {0.897, 0.565, 0.656, 0.1234, 0.665, 0.3434};
        int n = arr.length;
        Solution s = new Solution();
        s.bucketSort(arr, n);

        System.out.println("排序后的数组是:");
        for (int i = 0; i < n; ++i) {
            System.out.print(arr[i] + " ");
        }
    }
}
英文:

I'm using Java to implement the Bucket Sorting. I want to sort the input array of [0.897, 0.565, 0.656, 0.1234, 0.665, 0.3434] and I create the buckets as an array containing List&lt;Double&gt; as it's elements, I sort every List&lt;Double&gt; individually using List.sort and concatenate them to get the result.

But Error occurs when I use the ArrayList.sort method to sort the List. I use a Lambda Expression as the parameter of the sort function and get an error message from the IDE, it says Cannot infer functional interface type .

The error comes from this line:

buckets[i].sort((double a, double b) -&gt; (int)Math.signum(a-b));

But when I change it to

buckets[i].sort((a, b) -&gt; (int)Math.signum(a-b));

there is no error and the code works well.

I am very confused why it can't infer? Thanks in advance.

The entire code is here:

import java.util.ArrayList;
import java.util.List;
class Solution {
void buckerSort(double[] arr, int n){
//create the buckets
List&lt;Double&gt;[] buckets = new ArrayList[n];
for (int i = 0; i&lt;n; ++i){
buckets[i] = new ArrayList&lt;Double&gt;();
}
//add the input to the buckets
for (int i=0; i&lt;n; ++i) {
int index = (int) arr[i] * 10;
buckets[index].add(arr[i]);
}
//sort every List individually
///////////////////////////////The error occurs here/////////////////////////////////////////
for (int i=0; i&lt;n; ++i) {
buckets[i].sort((double a, double b) -&gt; (int)Math.signum(a-b));
}
//concatenate
int index = 0;
for(int i = 0; i&lt;n; i++) {
for (int j = 0; j&lt;buckets[i].size(); j++) {
arr[index] = buckets[i].get(j);
index++;
}
}
}
public static void main(String args[])
{
double[] arr = {0.897, 0.565, 0.656, 0.1234, 0.665, 0.3434};
int n = arr.length;
Solution s = new Solution();
s.buckerSort(arr, n);
System.out.println(&quot;Sorted array is: &quot;);
for (int i = 0; i &lt; n; ++i) {
System.out.print(arr[i] + &quot; &quot;);
}
}
}

答案1

得分: 2

你可以使用以下方式:

buckets[i].sort((Double a, Double b) -> (int) Math.signum(a - b));

因为Double类型的Comparator接受两个Double类型的参数,而不是原始的double类型参数。

更重要的是,你可能只是想要使用Comparator.naturalOrder()自然排序元素,因为执行减法并不是比较元素的好方法。所以你的代码会像这样:

buckets[i].sort(Comparator.naturalOrder());
英文:

You can use

buckets[i].sort( ( Double a, Double b ) -&gt; (int) Math.signum( a - b ) );

instead, since a Comparator of Double accepts two Double type arguments and not primitive double arguments.

public int compare( Double a, Double b )

More importantly, you might just be looking to sort the elements naturally using Comparator.naturalOrder() as performing a subtraction isn't a good way of comparing elements. So your code would look like -

buckets[i].sort( Comparator.naturalOrder() );

huangapple
  • 本文由 发表于 2020年7月22日 12:36:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/63026964.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定