• 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

store result of queue.poll in arraylist

 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
how can i store result of queue.poll in arraylist???

ArrayList<UrlResponseBean> arrurlInfo = null;
public static ConcurrentLinkedQueue<UrlResponseBean> queue = new ConcurrentLinkedQueue<UrlResponseBean>();

public void run() {
arrurlInfo = new ArrayList<UrlResponseBean>();
while(true)
{
if(queue.size()>=5)
{
for(int i=0;i<queue.size();i++)
{
arrurlInfo=queue.poll();
insertIntoDb(arrurlInfo);
}
}

}
}
 
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please UseCodeTags when posting code. It will highlight your code and make it much easier to read. It probably will also increase the number of people helping you.

what is wrong with:
But if you want to insert all of them you can use addAll()
 
Sheriff
Posts: 3063
12
Mac IntelliJ IDE Python VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Moved to our Beginning Java forum.
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Wouter Oet wrote:But if you want to insert all of them you can use addAll()


Followed by clear(). After all, pop() removes the element.

On a side note, the for-loop looks dangerous to me, since the body changes the result of queue.size(). Consider an initial queue size of 4:
i == 0, queue.size() == 4 so execute the body. Afterwards, queue.size() == 3.
i == 1, queue.size() == 3 so execute the body. Afterwards, queue.size() == 2.
i == 2, queue.size() == 2 -> quit

If you want to keep the manual loop and poll(), change it to a while loop:
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic