编辑和重新排序PriorityBlockingQueue中所有元素的最佳方法是什么?

时间:2016-10-26 18:52:54

标签: java priority-queue

我有一个优先级队列,我经常需要编辑其中的所有元素,然后重新排序整个队列。这是一个简单的代码来说明一种可能的解决方案。

package com.raby.shonguiz.zebi.test.collection;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Random;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.PriorityBlockingQueue;

import com.raby.shonguiz.zebi.test.CustomObject;

public class PriorityBlockingQueueTest {

    public static void main( String[] args ) throws InterruptedException {
        reorderQueue();
    }

    private static void reorderQueue() {
        int nbElements = 10;
        BlockingQueue<CustomObject> priorityBlockingQueue = createQueue( nbElements );
        Collection<CustomObject> col = new ArrayList<CustomObject>( priorityBlockingQueue.size() );
        priorityBlockingQueue.drainTo( col );
        for ( CustomObject cO : col ) {
            editElement( cO, nbElements );
            priorityBlockingQueue.add( cO );
        }
        pollQueue( priorityBlockingQueue );
    }

    private static void editElement( CustomObject cO, int nbElements ) {
        cO.setId( new Random().nextInt( nbElements ) );
    }



    public static BlockingQueue<CustomObject> createQueue( int nbItems ) {
        BlockingQueue<CustomObject> priorityBlockingQueue = new PriorityBlockingQueue<CustomObject>();
        for ( int i = 0; i < nbItems; i++ ) {
            Random rd = new Random();
            priorityBlockingQueue.add( new CustomObject( rd.nextInt( nbItems ) ) );
        }
        return priorityBlockingQueue;
    }

    public static void pollQueue( BlockingQueue<CustomObject> priorityBlockingQueue ) {
        while ( !priorityBlockingQueue.isEmpty() ) {
            System.out.println( priorityBlockingQueue.poll() );
        }
    }

}

这是CustomObject类

package com.raby.shonguiz.zebi.test;

public class CustomObject implements Comparable<CustomObject> {

    long id;

    public CustomObject() {

    }

    public CustomObject( long id ) {
        this.id = id;
    }

    public long getId() {
        return id;
    }

    public void setId( long id ) {
        this.id = id;
    }

    @Override
    public boolean equals( Object obj ) {
        if ( this == obj )
            return true;
        if ( obj == null )
            return false;
        if ( getClass() != obj.getClass() )
            return false;
        CustomObject other = (CustomObject) obj;
        if ( id != other.id )
            return false;
        return true;
    }

    @Override
    public String toString() {
        StringBuilder builder = new StringBuilder();
        builder.append( "CustomObject [id=" );
        builder.append( id );
        builder.append( "]" );
        return builder.toString();
    }

    @Override
    public int compareTo( CustomObject o ) {
        return Long.compare( id, o.getId() );
    }
}

所以我正在做的是我将队列耗尽到一个集合,然后编辑集合中的所有元素并重新插入它们,是否有更好的方法(性能更低)定期编辑和重新排序所有元素一个PriorityQueue?

0 个答案:

没有答案