Skip to content

Instantly share code, notes, and snippets.

@LuxXx
Last active April 29, 2017 13:34
Show Gist options
  • Save LuxXx/fd8c85375a66802d8b1342d16af0f728 to your computer and use it in GitHub Desktop.
Save LuxXx/fd8c85375a66802d8b1342d16af0f728 to your computer and use it in GitHub Desktop.
Comparator
import java.util.Comparator;
import java.util.PriorityQueue;
public class Comp {
public static void main(String[] args) {
PriorityQueue<Integer> q = new PriorityQueue<>(new Comparator<Integer>() {
@Override
public int compare(Integer o1, Integer o2) {
return sig(o1 - o2);
}
private int sig(int a) {
return a == 0 ? 0 : a > 0 ? 1 : -1;
}
});
// Lambda version
PriorityQueue<Integer> q2 = new PriorityQueue<>((a, b) -> a - b == 0 ? 0 : a - b > 0 ? 1 : -1);
for (int i = 0; i < 100; i++) {
q.add((int)(Math.random() * 1000));
}
System.out.println(q);
System.out.println(q.comparator().compare(0, 1));
System.out.println(q.comparator().compare(1, 0));
System.out.println(q.comparator().compare(0, 0));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment