How to use a loop to delete elements from a queue in Java
I have such a data structure:
I'm trying to do this:
for(Mail mail: mail@R_476_2419@) { if(badNews(mail)) { mail@R_476_2419@.remove(mail); } }
Obviously, the contents of the loop will interfere with the boundary and trigger errors, so I usually do this:
for(int i = 0; i < mail@R_476_2419@.size(); i++) { if(badNews(mail@R_476_2419@.get(i))) { mail@R_476_2419@.remove(i); i--; } }
Unfortunately, BlockingQueue doesn't have the ability to get or delete elements by index, so I'm stuck Any ideas?
Edit – some clarification: one of my goals is to keep the same order. It's not good to pop from the head and put it back to the tail In addition, although no other thread will delete messages from the mailbox, they will be added to the mailbox, so I don't want to be in the middle of the deletion algorithm, let someone send messages to me, and then an exception occurs
Thank you in advance!
Solution
You can complete all elements in the queue until all elements in the queue, and then you can use poll and P ̶ u ̶ s ̶ h ̶ offer. Here is an example:
Mail firstMail = mail@R_476_2419@.peek(); Mail currentMail = mail@R_476_2419@.pop(); while (true) { //a base condition to stop the loop Mail tempMail = mail@R_476_2419@.peek(); if (tempMail == null || tempMail.equals(firstMail)) { mail@R_476_2419@.offer(currentMail); break; } //if there's nothing wrong with the current mail,then re add to mail@R_476_2419@ if (!badNews(currentMail)) { mail@R_476_2419@.offer(currentMail); } currentMail = mail@R_476_2419@.poll(); }
Note that this method is only effective if the code is executed in a single thread and no other thread removes the item from this queue
Maybe you need to check if you really want to poll or get elements from BlockingQueue Provide and place similar
More information:
> Java BlockingQueue take() vs poll() > LinkedBlockingQueue put vs offer
Another less wrong approach is to use temporary collections, not necessarily concurrent, and store the elements you still need in the queue This is a startup example:
List<Mail> mailListTemp = new ArrayList<>(); while (mail@R_476_2419@.peek() != null) { Mail mail = mail@R_476_2419@.take(); if (!badNews(mail)) { mailListTemp.add(mail); } } for (Mail mail : mailListTemp) { mail@R_476_2419@.offer(mail); }