Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Return type from a Comparator

Tags:

java

What does the return value inside the Comparator actually mean?

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

[20, 10, 30, 100]

If the return type is -1 then its actually returning

[100, 30, 10, 20]

If the return type is 0 then its actually returning

[20]

Please tell me what does this indicate?

like image 203
Pawan Avatar asked Jun 25 '11 14:06

Pawan


People also ask

What should I return from Comparator?

It returns a positive value if obj1 is greater than obj2. Otherwise, a negative value is returned. By overriding compare( ), you can alter the way that objects are ordered. For example, to sort in a reverse order, you can create a comparator that reverses the outcome of a comparison.

What is the return type of Comparator in Java?

Java Comparator Interface Definition The compare() method returns an int which signals which of the two objects was larger. The semantics of the return values are: A negative value means that the first object was smaller than second object. The value 0 means the two objects are equal.

What is a Comparator in Java?

Java Comparator is an interface for sorting Java objects. Invoked by “java. util. comparator,” Java Comparator compares two Java objects in a “compare(Object 01, Object 02)” format. Using configurable methods, Java Comparator can compare objects to return an integer based on a positive, equal or negative comparison.


2 Answers

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

-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.

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.

like image 52
RHSeeger Avatar answered Sep 28 '22 07:09

RHSeeger


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

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

like image 25
Björn Pollex Avatar answered Sep 28 '22 08:09

Björn Pollex