如何按顺序迭代PriorityBlockingQueue

时间:2023-01-23 21:09:34

I have a PriorityBlockingQueue<Shirts> that uses a FIFO (first in first out) comparator in addition to other custom conditions. While obeying the priority, I need to pull out only the Green shirts. I understand the objective with a Queue is that they are intended for items to be "taken off of the top". The iterator() doesn't obey the order, so I can't use that. Should I be using a different queue altogether? How can I iterate over these items.

我有一个PriorityBlockingQueue ,除了其他自定义条件外,还使用FIFO(先进先出)比较器。在遵守优先权的同时,我只需要拔出绿色衬衫。我理解Queue的目标是它们用于“脱离顶部”的项目。 iterator()不遵守顺序,所以我不能使用它。我应该完全使用不同的队列吗?我该如何迭代这些项目。

2 个解决方案

#1


6  

PriorityQueue.iterator() doesn't obey the order but retrieval operations poll, remove, peek, and element access the element at the head of the queue. So, taking into consideration that PriorityBlockingQueue cannot have null elements, you can iterate the elements in order this way

PriorityQueue.iterator()不遵守顺序,但检索操作轮询,删除,查看和元素访问队列头部的元素。因此,考虑到PriorityBlockingQueue不能具有null元素,您可以按这种方式迭代元素

PriorityBlockingQueue<Shirts> q = ...
for (Shirts shirts; (shirts = q.poll()) != null; ) {
    ...      
}

Another way

其他方式

     Shirts[] a = q.toArray(new Shirts[q.size()]);
     Arrays.sort(a);

now you can iterate in both direction

现在你可以向两个方向迭代

#2


-1  

Taking a look at the javadoc, you should be able to use the "take" method.

看一下javadoc,你应该可以使用“take”方法。

public E take() throws InterruptedException Description copied from interface: BlockingQueue Retrieves and removes the head of this queue, waiting if necessary until an element becomes available.

public E take()throws InterruptedException从接口复制的描述:BlockingQueue检索并移除此队列的头部,必要时等待,直到元素可用。

Just throw a for loop around the queue and voila! You can iterate with no problems. :)

只是在队列周围抛出一个for循环,瞧!您可以毫无问题地进行迭代。 :)

Assuming the queue is strictly FIFO

假设队列是严格的FIFO

You can then use "put" to put the item back into the queue if it is not the green shirt

然后,如果它不是绿色衬衫,您可以使用“put”将项目放回队列中

public void put(E e) Inserts the specified element into this priority queue. As the queue is unbounded, this method will never block.

public void put(E e)将指定的元素插入此优先级队列。由于队列是无限制的,因此该方法永远不会阻塞。

This way the non-green shirts will still be in the queue in order (once the loop is finished).

这样,非绿色衬衫仍将按顺序排列在队列中(一旦循环结束)。

The way you have it setup

你设置的方式

Since you are using a PriorityBlockingQueue, putting the objects right back in the queue could eventually lead to the same item being pulled off the queue each time around the loop. You would need to create a new queue to put the item back into once it is checked to see if it is a green shirt.

由于您使用的是PriorityBlockingQueue,因此将对象放回队列中可能最终会导致每次循环时将相同的项目从队列中拉出。您需要创建一个新队列,以便在检查项目是否为绿色衬衫时将其重新放入。

Example code

示例代码

PriorityBlockingQueue queue = new PriorityBlockingQueue(); PriorityBlockingQueue backupqueue = new PriorityBlockingQueue();

PriorityBlockingQueue queue = new PriorityBlockingQueue(); PriorityBlockingQueue backupqueue = new PriorityBlockingQueue();

    queue.put("A");
    queue.put("B");
    queue.put("C");
    queue.put("D");

    List<String> saveList = new ArrayList<String>();
    int initialSize = queue.size();
    for(int i = 0; i < initialSize; i++){
        String element = queue.take();
        if(element.equals("C")){
            saveList.add(element);
        } else {
            backupqueue.put(element);
        }
    }

#1


6  

PriorityQueue.iterator() doesn't obey the order but retrieval operations poll, remove, peek, and element access the element at the head of the queue. So, taking into consideration that PriorityBlockingQueue cannot have null elements, you can iterate the elements in order this way

PriorityQueue.iterator()不遵守顺序,但检索操作轮询,删除,查看和元素访问队列头部的元素。因此,考虑到PriorityBlockingQueue不能具有null元素,您可以按这种方式迭代元素

PriorityBlockingQueue<Shirts> q = ...
for (Shirts shirts; (shirts = q.poll()) != null; ) {
    ...      
}

Another way

其他方式

     Shirts[] a = q.toArray(new Shirts[q.size()]);
     Arrays.sort(a);

now you can iterate in both direction

现在你可以向两个方向迭代

#2


-1  

Taking a look at the javadoc, you should be able to use the "take" method.

看一下javadoc,你应该可以使用“take”方法。

public E take() throws InterruptedException Description copied from interface: BlockingQueue Retrieves and removes the head of this queue, waiting if necessary until an element becomes available.

public E take()throws InterruptedException从接口复制的描述:BlockingQueue检索并移除此队列的头部,必要时等待,直到元素可用。

Just throw a for loop around the queue and voila! You can iterate with no problems. :)

只是在队列周围抛出一个for循环,瞧!您可以毫无问题地进行迭代。 :)

Assuming the queue is strictly FIFO

假设队列是严格的FIFO

You can then use "put" to put the item back into the queue if it is not the green shirt

然后,如果它不是绿色衬衫,您可以使用“put”将项目放回队列中

public void put(E e) Inserts the specified element into this priority queue. As the queue is unbounded, this method will never block.

public void put(E e)将指定的元素插入此优先级队列。由于队列是无限制的,因此该方法永远不会阻塞。

This way the non-green shirts will still be in the queue in order (once the loop is finished).

这样,非绿色衬衫仍将按顺序排列在队列中(一旦循环结束)。

The way you have it setup

你设置的方式

Since you are using a PriorityBlockingQueue, putting the objects right back in the queue could eventually lead to the same item being pulled off the queue each time around the loop. You would need to create a new queue to put the item back into once it is checked to see if it is a green shirt.

由于您使用的是PriorityBlockingQueue,因此将对象放回队列中可能最终会导致每次循环时将相同的项目从队列中拉出。您需要创建一个新队列,以便在检查项目是否为绿色衬衫时将其重新放入。

Example code

示例代码

PriorityBlockingQueue queue = new PriorityBlockingQueue(); PriorityBlockingQueue backupqueue = new PriorityBlockingQueue();

PriorityBlockingQueue queue = new PriorityBlockingQueue(); PriorityBlockingQueue backupqueue = new PriorityBlockingQueue();

    queue.put("A");
    queue.put("B");
    queue.put("C");
    queue.put("D");

    List<String> saveList = new ArrayList<String>();
    int initialSize = queue.size();
    for(int i = 0; i < initialSize; i++){
        String element = queue.take();
        if(element.equals("C")){
            saveList.add(element);
        } else {
            backupqueue.put(element);
        }
    }