title: cannot cast to java.lang.Comparable
body: <p>Although this question has already been asked but I have an implementation specific doubt.</p>
  <p>I am trying to print the top view of the binary tree and following is the complete code for it:</p>
  <pre><code>import java.util.*;
  
  class Node{
      int data;
      Node right;
      Node left;
      Node(int data){
          this.data = data;
      }
  }
  
  class Pair&lt;F,S&gt;{
      private F first;
      private S second;
      public Pair(F first, S second){
          this.first = first;
          this.second = second;
      }
      
      public F getFirst(){return first;}
      public S getSecond(){return second;}
  }
  
  class BinaryTreeTopView{
  
      public static void printTopView(Node root){
  
          if(root == null)
              return;
  
      Queue &lt;Pair&lt;Node,Integer&gt;&gt; q = new Queue&lt;&gt;();
      Map &lt;Integer,Node&gt; map = new HashMap&lt;&gt;();
      Pair&lt;Node,Integer&gt; p = new Pair&lt;&gt;(root, 0);
      q.add(p);
      
      /*
      I am storing nodes and the corresponding horizontal distances 
      in the form of a pair which then are being stored in the queue
      to ensure level order traversal
      */
  
      while(!q.isEmpty()){
          Pair&lt;Node,Integer&gt; temp = q.peek();
          q.remove();
  
          if(map.containsKey(temp.getSecond())==true){
              map.put(temp.getSecond(),temp.getFirst());
          } else {
              System.out.println(temp.getFirst().data);
              map.put(temp.getSecond(),temp.getFirst());                
          }
  
          if(temp.getFirst().left!=null){
              Pair&lt;Node,Integer&gt; left = new Pair&lt;&gt;(temp.getFirst().left, temp.getSecond()-1);
              q.add(left);
          }
          
          if(temp.getFirst().right!=null){
              Pair&lt;Node,Integer&gt; right = new Pair&lt;&gt; (temp.getFirst().right, temp.getSecond()+1);
              q.add(right);
          }
          
      }
  }
  public static void main(String[] args) {
      Node root = new Node(1);
      root.left = new Node(2);
      root.right = new Node(3);
      root.left.right = new Node(5);
      root.left.left = new Node(4);
      root.right.left = new Node(6);
      root.right.right = new Node(7);
      root.right.left.right = new Node(8);
      root.right.right.left = new Node(10);
      root.right.right.right = new Node(9);
      root.right.right.left.right = new Node(11);
      root.right.right.left.right.right = new Node(12);
      
      printTopView(root);
  }
  }
  </code></pre>
  <p>It compiles fine but an exception is being raised at the runtime.
  Now I have been getting the following exception and I am unable to figure out what the problem is:</p>
  <pre><code>Exception in thread &quot;main&quot; java.lang.ClassCastException: 
  Pair cannot be cast to java.lang.Comparable at  java.util.PriorityQueue.siftUpComparable(PriorityQueue.java:652)
  at java.util.PriorityQueue.siftUp(PriorityQueue.java:647)
  at java.util.PriorityQueue.offer(PriorityQueue.java:344)
  at java.util.PriorityQueue.add(PriorityQueue.java:321)
  </code></pre>