title: Java Priority Queue with ArrayLists and Pairs
body: <p>I'm looking for simple way to build a Priority Queue in Java. I've built an <code>ArrayList&lt;Pair&gt;</code>, and each <code>Pair</code> instance contains an <code>X</code> and <code>Y</code> values.</p>
  <pre class="lang-java prettyprint-override"><code>class Pair {
      private final Float xVal;
      private final Float yVal;
  
      public Pair(Float aXVal, Float aYVal) {
          xVal = aXVal;
          yVal = aYVal;
      }
  
      public float getX() {
          return xVal;
      }
  
      public float getY() {
          return yVal;
      }
  }
  </code></pre>
  <p>My <code>ArrayList</code> looks like:</p>
  <pre><code>ArrayList&lt;Pair&gt; listOfPoints;
  </code></pre>
  <p>Using the <code>ArrayList listOfPoints</code>, I wanted to build two priority queues:</p>
  <ul>
  <li>One that is sorted on the x Values from low to high.</li>
  <li>One that is sorted on the y Values form low to high.</li>
  </ul>
  <p>I was looking for a simple way to do this using Lambda expressions.</p>
  <p>I did look at <a href="https://stackoverflow.com/questions/683041/how-do-i-use-a-priorityqueue">this</a> question on Stack Overflow, and I found this code:</p>
  <pre><code>PriorityQueue&lt;String&gt; pq=
                      new PriorityQueue&lt;String&gt;(5,(a,b) -&gt; a.length() - b.length());
  </code></pre>
  <p>I think this is close to what I want.</p>
  <p>I was trying to implement the following:</p>
  <pre><code>PriorityQueue&lt;Pair&gt; xSorted = new PriorityQueue&lt;Pair&gt;(numOfPoints, (x1,x2) -&gt; Need Help Here);
  </code></pre>
  <p>How do I access Pair in order to have it compare <code>x1</code> and <code>x2</code>?</p>
  <p>Note, that <code>nummberOfPoints</code> I was setting to the length of <code>ArrayList&lt;Pair&gt; listOfPoints</code>.</p>