Question: The following program returns result “1″, which indicates that first Integer value is greater than the second, why?
import java.util.*; public class Example { public static void main(String[] args) { System.out.println("Result: " + naturalOrder.compare(new Integer(90), new Integer(90))); } private static Comparator<Integer> naturalOrder = new Comparator<Integer>() { public int compare(Integer first, Integer second) { return first < second ? -1 : (first == second ? 0 :1); } }; }
Please note:
In this case, comparator for natural order on Integer is written for example only, and in practice there is no need to write it.
Looking forward for your answers dear readers
Resources:
Effective Java