Java 比较器的返回类型

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6478515/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-16 06:17:01  来源:igfitidea点击:

Return type from a Comparator

java

提问by Pawan

What does the return value inside the Comparator actually mean?

Comparator 中的返回值实际上是什么意思?

For example :

例如 :

class TreeSetDemo
{
    public static void main(String arg[])
    {
        TreeSet t=new TreeSet(new MyComparator());
        t.add(new Integer(20));
        t.add(new Integer(10));
        t.add(new Integer(30));
        t.add(new Integer(100));
        System.out.println(t); 
    }    

    class MyComparator implements Comparator 
    {    
        public int compare(Object o1, Object o2) 
        {
            return 0;
        }
    }
}

If the return type is 1 then its actually returning

如果返回类型为 1 那么它实际返回

[20, 10, 30, 100]

[20, 10, 30, 100]

If the return type is -1 then its actually returning

如果返回类型是 -1 那么它实际返回

[100, 30, 10, 20]

[100, 30, 10, 20]

If the return type is 0 then its actually returning

如果返回类型为 0 那么它实际返回

[20]

[20]

Please tell me what does this indicate?

请告诉我这说明什么?

采纳答案by RHSeeger

The return value(not type, the type is int) tells the caller (the thing sorting the data):

返回value(不是type,类型是int)告诉调用者(对数据进行排序的东西):

-1 : o1 < o2
0 : o1 == o2
+1 : o1 > o2

If you always return the same value (o, 1, -1) for the comparator, regardless of it's inputs, then you're not using it correctly. You need to base the value returned on the values passed in. The idea is that the data structure (or sorter) calls the comparison function any time it needs to order two elements, to find out what order to put them in.

如果你总是为比较器返回相同的值 (o, 1, -1),不管它是什么输入,那么你没有正确使用它。您需要根据传入的值返回值。这个想法是数据结构(或排序器)在需要对两个元素进行排序时调用比较函数,以找出将它们放入的顺序。

Its worth noting that the positive/negative integer values (-1, +1) don't need to be 1, they can be any positive/negative numbers. It's just common practice to return -1/+1.

值得注意的是,正/负整数值 (-1, +1) 不需要为 1,它们可以是任何正/负数。返回 -1/+1 只是常见的做法。

回答by u290629

It's about sort algorithm which needs compare.

这是关于需要比较的排序算法。

Correct:

正确的:

class MyComparator implements Comparator<Integer> {
    public int compare(Integer o1, Integer o2) {
        return o1.complare(o2);
    }
}

回答by Bj?rn Pollex

You are confusing return-typeand return-value. The return-type is int. The return value is described in the documentation:

您混淆了return-typereturn-value。返回类型是int文档中描述了返回值:

Returns a negative integer, zero, or a positive integer as the first argument is less than, equal to, or greater than the second.

当第一个参数小于、等于或大于第二个参数时,返回一个负整数、零或正整数。

回答by Dunes

Comparator documentation-- Returns: a negative integer, zero, or a positive integer as the first argument is less than, equal to, or greater than the second.

比较器文档——返回:负整数、零或正整数,因为第一个参数小于、等于或大于第二个参数。

回答by Kevin Bowersox

Depending on how you want to sort based on this comparator you need to place some logic in the comparator. Your comparator only returns 0 which means equal

根据您希望基于此比较器的排序方式,您需要在比较器中放置一些逻辑。您的比较器只返回 0,这意味着相等

class MyComparator implements Comparator {

    public int compare(Object o1, Object o2) {
        // TODO Auto-generated method stub
        return o1.compareTo(o2);
    }
}

回答by Nikith9493

the way comparator is leveraged here is wrong but for clarifying your doubt why only [20] id being written when return value is 0...

这里利用比较器的方式是错误的,但为了澄清您的疑问,为什么当返回值为 0 时只写入 [20] id ...

You are using a TreeSetwhich doesn't hold identical values(Property of Set). So when the values are compared and return value is zero, Java treats them as equal and retain only the first value. Hence, you are seeing only one value in return.

您正在使用TreeSet不具有相同值(集合属性)的 。因此,当比较值并且返回值为零时,Java 将它们视为相等并仅保留第一个值。因此,您只会看到一个回报值。