Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

store result of queue.poll in arraylist

 
fioana elegant
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • 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);
}
}

}
}
 
Wouter Oet
Saloon Keeper
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
  • 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()
 
Greg Charles
Sheriff
Posts: 2989
12
Firefox Browser IntelliJ IDE Java Mac Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Moved to our Beginning Java forum.
 
Rob Spoor
Sheriff
Pie
Posts: 20605
60
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • 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:
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic