title: Can someone explain PriorityQueue in this example to me?
body: <p>I am trying to learn how to use a PriorityQueue, as I have never used one before. Here is an example of one in use that I have found on LeetCode for a problem that finds the Top K elements in an array of strings</p>
  
  <pre><code>public List&lt;String&gt; topKFrequent(String[] words, int k) {
      Map&lt;String, Integer&gt; count = new HashMap();
      for (String word: words) {
          count.put(word, count.getOrDefault(word, 0) + 1);
      }
      PriorityQueue&lt;String&gt; heap = new PriorityQueue&lt;String&gt;(
              (w1, w2) -&gt; count.get(w1).equals(count.get(w2)) ?
              w2.compareTo(w1) : count.get(w1) - count.get(w2) );
  
      for (String word: count.keySet()) {
          heap.offer(word);
          if (heap.size() &gt; k) heap.poll();
      }
  
      List&lt;String&gt; ans = new ArrayList();
      while (!heap.isEmpty()) ans.add(heap.poll());
      Collections.reverse(ans);
      return ans;
  }
  </code></pre>
  
  <p>More notably I would like to know what this line is doing:</p>
  
  <pre><code>PriorityQueue&lt;String&gt; heap = new PriorityQueue&lt;String&gt;(
              (w1, w2) -&gt; count.get(w1).equals(count.get(w2)) ?
              w2.compareTo(w1) : count.get(w1) - count.get(w2) );
  </code></pre>
  
  <p>Can someone explain in lame-man's terms what is happening here? Is there any way to rewrite the comparater as a regular "if" statement?</p>
  
  <p>Thanks for any help.</p>