Note that there are some explanatory texts on larger screens.

plurals
  1. POJava: what the return does here?
    primarykey
    data
    text
    <p>I founded this code on the <a href="http://www.algorithmbase.com/258/java-priority-queue-example/" rel="nofollow">web</a> . Excuse-me if a ask here,but I haven't only understand one thing. What does exactly <strong><em>return population2 - population1;</em></strong>? It sorts the items by comparing every time each other (<strong>o1</strong> and <strong>o2</strong>) , by taking the part after the colon without any eventual spaces (leadings and finals) , so the numbers, and sorting them by...?</p> <pre><code>import java.util.Comparator; import java.util.PriorityQueue; public class Main { public static void main(String[] args) { PriorityQueue&lt;String&gt; queue = new PriorityQueue&lt;String&gt;(11, new Comparator&lt;String&gt;() { public int compare(String o1, String o2) { int population1 = Integer.parseInt(o1.split(":")[1].trim()); int population2 = Integer.parseInt(o2.split(":")[1].trim()); return population2 - population1; } }); queue.add("United States: 307006550"); queue.add("Brazil: 193733800"); queue.offer("Russia: 141850000"); queue.offer("India: 1155347700"); queue.offer("China: 1331460000"); System.out.println("Countries in database: " + queue.size()); while (!queue.isEmpty()) { System.out.println(queue.poll()); } System.out.println("Countries in database: " + queue.size()); } } </code></pre> <p>If i try to change, for example the code to:</p> <pre><code>return population1 - population2; </code></pre> <p>it orders the numbers as:</p> <pre><code>United States: 307006550 Russia: 141850000 Brazil: 193733800 India: 1155347700 China: 1331460000 </code></pre> <p>Why?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload