Java collection framework- PriorityBlockingQueue

Java collection framework- PriorityBlockingQueue


Posted in : Core Java Posted on : May 16, 2011 at 6:24 PM Comments : [ 0 ]

The PriorityBlockingQueue� is a queue that have unbounded capacity which provides a data structure to store object in their priority, priority of these objects may be on their natural order

Java collection framework- PriorityBlockingQueue

Sometimes we need to maintain an object in a priority based. The PriorityBlockingQueue is a queue that have unbounded capacity which provides a data structure to store object in their priority, priority of these objects may be on their natural order or depends on comparator provided at construction time just as the class PriorityQueue does. A PriorityBlockingQueue is unbounded though it blocks the retrieval operations and the addition operation may get failed because of resource exhaustion. An entry of 'null' element is not allowed by this class. The guarantee of traversing the elements of PriorityBlockingQueue in any specified order is not taken by the iterator() method of the Interface Iterator. If it is required to traveling the elements in an ordered manner then consider Arrays.sort(pq.toArray()). In this class there is not a specific criteria to work on the equal priority element if you required to distinguish the equal priorities element you may define your custom classes or comparators in which a key will break ties between the primary priority values.

Syntax

public class PriorityBlockingQueue<E> extends AbstractQueue<E> implements BlockingQueue<E>, Serializable

Constructor of PriorityBlockingQueue :

Here we are discussing about the constructor of PriorityBlockingQueue through which we can create objects according to the requirement. These constructor are :

  1. PriorityBlockingQueue() : This constructor creates a queue with default size. The default initial capacity of this queue is 11 and it orders the elements according to their natural order.
  2. PriorityBlockingQueue( Collection c) : This constructor creates a queue that contains the elements in a specified collection.
  3. PrirityBlockingQueue(int initialCapacity) : This constructor creates a queue with the specified size.
  4. PriorityBlockingQueue(int initialCapacity, Comparator cmp) : This constructor creates a queue with the initial specified size and elements of this queue are ordered according to the comparator.

This class also provides methods some of them which are commonly used are as follows :

  1.   add(E e) :
  2.              Syntax:               public boolean add(E e)

  3. iterator() :
  4.              Syntax                public Iterator<E> iterator()

  5. poll() :
  6.               Syntax                public E poll()

  7. remove(Object o) :
  8.                Syntax               public boolean remove(Object o)

  9. size() :
  10.                Syntax               public int size()

Example :

package devmanuals.com;
import java.util.concurrent.PriorityBlockingQueue;
import java.util.Iterator;
public class PriorityBlockingQueueDemo {
  public static void main(String args[]) {
    PriorityBlockingQueue<Integer> pbq1 = new PriorityBlockingQueue<Integer>();
    PriorityBlockingQueue<Integer> pbq2 = new PriorityBlockingQueue<Integer>();
    pbq1.add(11);
    pbq1.put(14);
    pbq1.offer(13);
    pbq1.offer(12);
    System.out.println("Elements of PBQ1 = " + pbq1);
    int i = pbq1.size();
    System.out.println("Size of PBQ1 = " + i);
    pbq2.offer(1);
    pbq2.put(2);
    pbq2.add(5);
    pbq2.add(3);
    pbq2.add(4);
    pbq2.add(6);
    pbq2.add(7);
    System.out.println("Elements of PBQ2 = " + pbq2);
    int i1 = pbq2.size();
    System.out.println("Size of PBQ2 = " + i1);
    pbq1.drainTo(pbq2);
    System.out.println("pbq1 = " + pbq1);
    System.out.println("pbq2 = " + pbq2);
    Iterator it = pbq2.iterator();
    System.out.println("Elements of PBQ2 using an iterator :");
    while (it.hasNext()) {
      System.out.println(it.next());
    }
  }
}

Output :

Elements of PBQ1 = [11, 12, 13, 14]

Size of PBQ1 = 4

Elements of PBQ2 = [1, 2, 5, 3, 4, 6, 7]

Size of PBQ2 = 7

pbq1 = []

pbq2 = [1, 2, 5, 3, 4, 6, 7, 11, 12, 13, 14]

Elements of PBQ2 using an iterator :

1

2

5

3

4

6

7

11

12

13

14

Download Source Code

Go to Topic «PreviousHomeNext»

Your Comment:


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 

 
Tutorial Topics