Q:

Java generics type parameter confusion

I've created a PriorityQueue which holds PeekingSortedIterators as follows:
PriorityQueue<PeekingSortedIterator<E>> pq= new PriorityQueue<>(iterators.size(), new IteratorComparator<E>());
pq.offer(new PeekingSortedIterator<E>(si));

The IteratorComparator compares the values underlying the PeekingSortedIterator. My code is as follows:
class IteratorComparator<E extends Comparable<E>> implements Comparator<PeekingSortedIterator<E>>{ // note generics!!!
    @Override
    public int compare(PeekingSortedIterator<E> o1, PeekingSortedIterator<E> o2) {
        return o1.peek().compareTo(o2.peek());
    } 
 }

My questions are as follows:

Why is the type parameter for IteratorComparator <E extends Comparable<E>>  and not <PeekingSortedIterator<E>> since the class operates on PeekingSortedIterator<E>, not directly on E? I understand that if I did that, then I would need a different way to specify that E needs to extend Comparable, but I'm confused because with IteratorComparator<E extends Comparable<E>> it seems like the compare method should have been compare(E e1, E e2).
Why is the IteratorComparator instance created with new IteratorComparator<E>()? Why do I get a compile time error (Type mismatch: cannot convert from PriorityQueue<PeekingSortedIterator<PeekingSortedIterator<E>>> to PriorityQueue<PeekingSortedIterator<E>>) if I modify it to new IteratorComparator<PeekingSortedIterator<E>>()?

Thanks in advance!

A:

You must understand that E in IteratorComparator<E extends Comparable<E>> is not a concrete type but a type variable.
The line
class IteratorComparator<E extends Comparable<E>> 
    implements Comparator<PeekingSortedIterator<E>>{

declares a class IteratorComparator for some type E that is Comparable<E> (comparable to itself, for example String or Integer). This class implements Comparator<PeekingSortedIterator<E>>, meaning that it can compare two PeekingSortedIterator<E>s

