简体   繁体   中英

What does this mean <E, P extends Comparable<? super P>>?

Anyways, I have been given a class and a PriorityQueueInterface to implement a Linked Node PriorityQueue. However, I'm having trouble grasping the class type. here is the Entry class

I just don't know how to start the assignment without knowing what this means.

public class Entry<E, P extends Comparable<? super P>>
         implements Comparable<Entry<E, P>>
{
    private E theItem; 
    private P thePriority; 

    public Entry(E item, P priority)
    {
        theItem = item; 
        thePriority = priority;
    } 

    public E getItem()
    {
        return theItem;
    } 

    public P getPriority()
    {
        return thePriority;
    } 

    public int compareTo(Entry<E, P> other)
    {
        return thePriority.compareTo(other.thePriority);
    }

    public String toString()
    {
        return "item/priority <" + theItem + ", " + thePriority + ">";
    }
} 

Here is the Interface

public interface PriorityQueueInterface<T extends Comparable<? super T> > {

/** Adds a new entry to this priority queue. 
 * @param newEntry An object to be added */ 
 public void add(T newEntry); 

 /** Removes and returns the entry having the highest priority. 
  * @return Either the object having the highest priority or 
  *         if, the priority queue is empty before the operation, null. */ 
  public T remove(); 

  /** Retrieves the entry having the highest priority.
  @return  Either the object having the highest priority or,
           if the priority queue is empty, null. */
  public T peek(); 

  /** Detects whether this priority queue is empty.
  @return  True if the priority queue is empty, or false otherwise. */
  public boolean isEmpty(); 

   /** Gets the size of this priority queue.
  @return  The number of entries currently in the priority queue. */
  public int getSize(); 

  /** Removes all entries from this priority queue. */
   public void clear();

}// End of PriorityQueueInterface

Let's break this down: Entry<E, P extends Comparable<? super P>> implements Comparable<Entry<E, P>> Entry<E, P extends Comparable<? super P>> implements Comparable<Entry<E, P>> . Think of Entry is a type (say A ). So the statement translates to A implements Comparable<A> . This means, this type can compare itself to other object of same type.

Now let's go deeper. Entry has two parameters. E and P . Easy.

Going further, P extends Comparable meaning P can compare itself to something. The type P can compare itself to is given by the innermost <> which is ? super P ? super P . This means P can compare itself to objects of type P or it's super class.

Putting everything together, you have an Entry of two parameters which should be able to compare itself to other entries of same parameters. One of those parameters is E . The other one is P and P should be able to compare itself to any of it's super class objects.

If you want to learn about when to write super and when to write extends , there are plenty of questions explaining that.

The concrete class for P in Entry must implement Comparable . Since Comparable is a generic as well, the declaration forces that P must implement Comparable over P with the <? super P> <? super P> part.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM