Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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