• Post Reply 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

why to use pollFirst() method?

 
Ranch Hand
Posts: 774
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dear All,

I am back to java-ranch after a break of almost one week, so back on scjp preparation.

I was reading chapter 7 from K&b and the topic regarding NavigableSet and NavigableMap.
Here it says, poll methods are also introduced in NavigableSet for example pollFirst() method
retrieves and removes the element?

Well, i can understand that. What i was looking forward to know, what is the practical use of such a method?
Why we need it?

Cheers,
 
Sheriff
Posts: 7135
1360
IntelliJ IDE jQuery Eclipse IDE Postgres Database Tomcat Server Chrome Google App Engine
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In general, you can use it on some specific situations where you feel you should use it. For an example, suppose a scenario like this - suppose that you have a Java web based application, and you need to display something like "quotation of the hour" on the home page. You get these quotations via RSS feeds of some other external sources, and the quotation on your home page should be updated once per hour. In this case, you can maintain a collection. Your program adds new quotations to the collection as soon as it gets some new quotations via RSS feeds - so the the collection will never get empty. At the begining of each hour, another thread can get a quotation from that collection and display it on the home page. If you don't need to display the same quotation for many times, you can use the pollFirst() method to get the quotation for each hour. It simply gets a quotation from the collection and removes it - so you don't get that same quotation again.

I know it isn't much practical as you may not do all of these things just on a collection instance. But hope you got an idea of how it can be used usefully

Devaka
 
Prithvi Sehgal
Ranch Hand
Posts: 774
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dear Devaka,

Thanks for your response. I think so by providing such an example, you have clarified my doubts completely
about pollFirst() essence.

Best Regards,
 
Ranch Hand
Posts: 142
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Just to add:

pollFirst() method could be typically used if you want to use a stack data structure to solve (mathematical) problems. A great example where pollFirst() could be used is to convert infix expressions into postfix expressions and also to evaluate postfix expressions by pushing and popping operators and operands into and from a stack (this is how the compiler does it). Here you always want to pollFirst() your top two operands on the stack, so that you can apply the operator to evaluate the value.

To learn more about postfix expressions or to look at an example search "postfix expression evaluation using stack" in Google. It is a typical thing you will study if you take the "Data Structures" course in Computer Science.

Hope this helps.
 
Prithvi Sehgal
Ranch Hand
Posts: 774
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Larry,

Thank you very much for providing such a detailed explanation. I will try to implement also what you suggested.
Yes in Data Structures, this is a very common example to solve.

Best Regards,
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic