title: Having trouble Implementing a Custom Comparator for a TreeSet (Dijkstra's)
body: <p>I'm currently trying and implementing my own custom O(N.lgN) solution of Dijkstra's using Adjacency Lists. Now if you are familiar with this algorithm (most likely you are), I was having trouble storing the tuple for each Vertex. If you have no clue what i'm talking about, have alook at: <a href="http://qr.ae/LoESY" rel="nofollow noreferrer">http://qr.ae/LoESY</a></p>
  
  <p>Tuples can easily be stored in C++ using </p>
  
  <pre><code>pair &lt;int,int&gt;.
  </code></pre>
  
  <p>Anyways, i found a solution to that and came to know, against all odds, that a similar class DOES exist its called the 'AbstractMap.SimpleEntry' Class. Details are given here:</p>
  
  <p><a href="https://stackoverflow.com/a/11253710/4258892">https://stackoverflow.com/a/11253710/4258892</a></p>
  
  <p>Now that you've read it, this works almost the same as pair&lt;> and suffices to store the Adjacent Edge as the key and Weight as the Value in the tuple.</p>
  
  <p>Declaration: Map.Entry pair= new AbstractMap.SimpleEntry(1,2);</p>
  
  <p>Now i have all the inputs in the form of tuples stored in an ArrayList for each vector. I planned on adding the tuples of the source entered to the TreeSet and sort them in ascending order wrt the weights (right?). However, if i just add these tuples to the TreeSet, I am throw an error:</p>
  
  <pre><code>Exception in thread "main" java.lang.ClassCastException:java.util.AbstractMap$SimpleEntry cannot be cast to java.lang.Comparable
  </code></pre>
  
  <p>Now i don't know how to implement a custom comparator for a TreeSet which sorts my values ascendingly (In Dijkstra's, the edge with least weight would come out first right?). Also, if not possible with TreeSet, could you provide me with a Priority Queue with a comparator implemented?</p>
  
  <p>Even if you didn't follow, Here's my code. Hopefully you'll understand:</p>
  
  <p><strong>EDIT</strong> Code Edited as per suggestion from below answer</p>
  
  <pre><code>package GRAPH;
  
  import java.io.*;
  import java.util.*;
  
  /**
   * Created by Shreyans on 3/25/2015 at 7:26 PM using IntelliJ IDEA (Fast IO Template)
   */
  
  class DIJKSTA_TRY
  {
      public static void main(String[] args) throws Exception
      {
          InputReader in = new InputReader(System.in);
          OutputWriter out = new OutputWriter(System.out);
          //Initializing Graph
          List&lt;ArrayList&lt;AbstractMap.SimpleEntry&lt;Integer,Integer&gt;&gt;&gt;gr=new ArrayList&lt;ArrayList&lt;AbstractMap.SimpleEntry&lt;Integer, Integer&gt;&gt;&gt;();//AbstractMap.SimpleEntry&lt;Integer,Integer&gt; is similar to pair&lt;int a,int b&gt; in C++
          System.out.println("Enter no of Vertices");
          int v=in.readInt();
          System.out.println("Enter no of Edges");
          int e=in.readInt();
          for(int i=0;i&lt;=v;i++)//Initializing rows for each vertex
          {
              gr.add(new ArrayList&lt;AbstractMap.SimpleEntry&lt;Integer, Integer&gt;&gt;());
          }
          System.out.println("Enter &lt;Vertex&gt; &lt;Adjacent Vertex&gt; &lt;Weight&gt;");
          for(int i=0;i&lt;e;i++)
          {
              int a = in.readInt();
              int b = in.readInt();
              int c = in.readInt();
              gr.get(a).add(new AbstractMap.SimpleEntry&lt;Integer, Integer&gt;(b, c));
          }
          out.printLine(gr);
          System.out.printf("Enter Source");
          int s=in.readInt();
          Comparator&lt;AbstractMap.SimpleEntry&lt;Integer, Integer&gt;&gt; comparator=new WeightComparator();
          TreeSet&lt;AbstractMap.SimpleEntry&lt;Integer, Integer&gt;&gt;ts=new TreeSet&lt;AbstractMap.SimpleEntry&lt;Integer, Integer&gt;&gt;(comparator);
          for(AbstractMap.SimpleEntry&lt;Integer, Integer&gt; pair: gr.get(s))//Error:Exception in thread "main" java.lang.ClassCastException: java.util.AbstractMap$SimpleEntry cannot be cast to java.lang.Comparable
          {
              ts.add(pair);
          }
          out.printLine(ts);
          {
              out.close();
          }
      }
  
      static public class WeightComparator implements
              Comparator&lt;AbstractMap.SimpleEntry&lt;Integer, Integer&gt;&gt;
      {
          @Override
          public int compare(AbstractMap.SimpleEntry&lt;Integer, Integer&gt; one,
                             AbstractMap.SimpleEntry&lt;Integer, Integer&gt; two)
          {
              return Integer.compare(one.getValue(), two.getValue());
          }
      }
      //FAST IO
      private static class InputReader
      {
          private InputStream stream;
          private byte[] buf = new byte[1024];
          private int curChar;
          private int numChars;
          private SpaceCharFilter filter;
  
          public InputReader(InputStream stream)
          {
              this.stream = stream;
          }
  
          public int read()
          {
              if (numChars == -1)
                  throw new InputMismatchException();
              if (curChar &gt;= numChars)
              {
                  curChar = 0;
                  try
                  {
                      numChars = stream.read(buf);
                  } catch (IOException e)
                  {
                      throw new InputMismatchException();
                  }
                  if (numChars &lt;= 0)
                      return -1;
              }
              return buf[curChar++];
          }
  
          public int readInt()
          {
              int c = read();
              while (isSpaceChar(c))
                  c = read();
              int sgn = 1;
              if (c == '-')
              {
                  sgn = -1;
                  c = read();
              }
              int res = 0;
              do
              {
                  if (c &lt; '0' || c &gt; '9')
                      throw new InputMismatchException();
                  res *= 10;
                  res += c - '0';
                  c = read();
              } while (!isSpaceChar(c));
              return res * sgn;
          }
  
          public String readString()
          {
              int c = read();
              while (isSpaceChar(c))
                  c = read();
              StringBuilder res = new StringBuilder();
              do
              {
                  res.appendCodePoint(c);
                  c = read();
              } while (!isSpaceChar(c));
              return res.toString();
          }
  
          public double readDouble()
          {
              int c = read();
              while (isSpaceChar(c))
                  c = read();
              int sgn = 1;
              if (c == '-')
              {
                  sgn = -1;
                  c = read();
              }
              double res = 0;
              while (!isSpaceChar(c) &amp;&amp; c != '.')
              {
                  if (c == 'e' || c == 'E')
                      return res * Math.pow(10, readInt());
                  if (c &lt; '0' || c &gt; '9')
                      throw new InputMismatchException();
                  res *= 10;
                  res += c - '0';
                  c = read();
              }
              if (c == '.')
              {
                  c = read();
                  double m = 1;
                  while (!isSpaceChar(c))
                  {
                      if (c == 'e' || c == 'E')
                          return res * Math.pow(10, readInt());
                      if (c &lt; '0' || c &gt; '9')
                          throw new InputMismatchException();
                      m /= 10;
                      res += (c - '0') * m;
                      c = read();
                  }
              }
              return res * sgn;
          }
  
          public long readLong()
          {
              int c = read();
              while (isSpaceChar(c))
                  c = read();
              int sgn = 1;
              if (c == '-')
              {
                  sgn = -1;
                  c = read();
              }
              long res = 0;
              do
              {
                  if (c &lt; '0' || c &gt; '9')
                      throw new InputMismatchException();
                  res *= 10;
                  res += c - '0';
                  c = read();
              } while (!isSpaceChar(c));
              return res * sgn;
          }
  
          public boolean isSpaceChar(int c)
          {
              if (filter != null)
                  return filter.isSpaceChar(c);
              return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
          }
  
          public String next()
          {
              return readString();
          }
  
          public interface SpaceCharFilter
          {
              public boolean isSpaceChar(int ch);
          }
      }
  
      private static class OutputWriter
      {
          private final PrintWriter writer;
  
          public OutputWriter(OutputStream outputStream)
          {
              writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream)));
          }
  
          public OutputWriter(Writer writer)
          {
              this.writer = new PrintWriter(writer);
          }
  
          public void print(Object... objects)
          {
              for (int i = 0; i &lt; objects.length; i++)
              {
                  if (i != 0)
                      writer.print(' ');
                  writer.print(objects[i]);
              }
          }
  
          public void printLine(Object... objects)
          {
              print(objects);
              writer.println();
          }
  
          public void close()
          {
              writer.close();
          }
  
          public void flush()
          {
              writer.flush();
          }
      }
  }
  
  Enter Source1
  [[], [2=3, 4=3], [3=4], [1=7], [3=2]]
  [2=3]
  </code></pre>
  
  <p><strong>EDIT</strong> Works. Thanks @rgettman</p>