• Post Reply Bookmark Topic Watch Topic
  • New Topic

store result of queue.poll in arraylist  RSS feed

 
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);
}
}

}
}
 
Bartender
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()
 
Sheriff
Posts: 3015
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.
 
Sheriff
Posts: 21137
87
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:
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!