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
loading...
Related posts:
- Brainteaser: Overridable methods
Consider the following case of inheritance: public class Parent { public Parent() { getValue(); } public void getValue() { } } public class... - Brainteaser: Hidden Iterators
… While locking can prevent iterators from throwing ConcurrentMofdificationException, You have to remember to use locking everywhere a shared collection might be iterated.... - Brainteaser: Broken Case of Inheritance
Consider the following case of inheritance: public class ExtendingHashSet<E> extends HashSet<E> { private int counter = 0; public ExtendingHashSet() { } @Override public... - How to Set SecurityManager and Java Security Policy Programmatically
In this example I want to show how to use SecurityManager to prevent unauthorized access to private members of a Java class, for... - Hack any Java class using reflection attack
Have you ever thought how secure your application is? Well reflection attack can demonstrate how vulnerable Java classes are. In this post, I...
