Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

question regarding PriorityQueue

 
Ranch Hand
Posts: 198
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
i got this question from the SCJP 5 book. can some one please explain to me why the value 10 has been put in the line of the code which i have commmented out below. and how does the comapre method check the Integers to determine that they must go in reverse order? i just do not understand the logic behind the compare method. please help

import java.util.*;

class pq {
static class pqsort implements Comparator<Integer>{
public int compare(Integer one, Integer two){
return two - one;

}

}

public static void main(String ar[]){

int []ia = {1,5,3,7,6,9,8};
PriorityQueue<Integer> pq1 = new PriorityQueue<Integer>();
for(int x:ia)
pq1.offer(x);
for(int x: ia)
System.out.print(pq1.poll()+" ");

System.out.println("");

pqsort pqs = new pqsort();
//PriorityQueue<Integer> pq2 = new PriorityQueue<Integer>(10,pqs);

for(int x: ia)
pq2.offer(x);
System.out.println("size"+ pq2.size());
System.out.println("peek "+ pq2.peek());
System.out.println("size "+pq2.size());
System.out.println("poll "+pq2.poll());
System.out.println("size "+pq2.size());

for(int x: ia)
System.out.println(pq2.poll());
}
}
thank you
Dinuka Arseculeratn
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
Please don't cross-post. It makes people reluctant to spend time helping when they realize the question has already been answered in another forum. Since this is from an SCJP book, please continue this discussion under the cross-post in the SCJP forum.
 
They worship nothing. They say it's because nothing lasts forever. Like this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
    Bookmark Topic Watch Topic
  • New Topic