Skip to content

Instantly share code, notes, and snippets.

@amaembo
Created December 14, 2020 09:55
Show Gist options
  • Save amaembo/15438fe72a00f842a4195ffd7367731b to your computer and use it in GitHub Desktop.
Save amaembo/15438fe72a00f842a4195ffd7367731b to your computer and use it in GitHub Desktop.
Which collection inherits comparator?
import java.util.Collection;
import java.util.Comparator;
import java.util.PriorityQueue;
import java.util.SortedSet;
import java.util.TreeSet;
import java.util.concurrent.PriorityBlockingQueue;
public class WhoInheritsComparator {
public static void main(String[] args) {
SortedSet<Integer> c1 = new TreeSet<>(Comparator.reverseOrder());
Collection<Integer> c2 = new TreeSet<>(Comparator.reverseOrder());
Collection<Integer> c3 = new PriorityQueue<>(Comparator.reverseOrder());
Collection<Integer> c4 = new PriorityBlockingQueue<>(10, Comparator.reverseOrder());
System.out.println(" TreeSet: " +
(new TreeSet<>(c1).comparator() != null) + "\t" + // inherits
(new TreeSet<>(c2).comparator() != null) + "\t" + // not inherits
(new TreeSet<>(c3).comparator() != null) + "\t" + // not inherits
(new TreeSet<>(c4).comparator() != null)); // not inherits
System.out.println(" PriorityQueue: " +
(new PriorityQueue<>(c1).comparator() != null) + "\t" + // inherits
(new PriorityQueue<>(c2).comparator() != null) + "\t" + // inherits
(new PriorityQueue<>(c3).comparator() != null) + "\t" + // inherits
(new PriorityQueue<>(c4).comparator() != null)); // not inherits
System.out.println("PriorityBlockingQueue: " +
(new PriorityBlockingQueue<>(c1).comparator() != null) + "\t" + // inherits
(new PriorityBlockingQueue<>(c2).comparator() != null) + "\t" + // inherits
(new PriorityBlockingQueue<>(c3).comparator() != null) + "\t" + // not inherits
(new PriorityBlockingQueue<>(c4).comparator() != null)); // inherits
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment