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.
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 :
Output
Syntax
public class PriorityBlockingQueue<E> extends AbstractQueue<E> implements BlockingQueue<E>, SerializableConstructor of PriorityBlockingQueue :
Here we are discussing about the constructor of PriorityBlockingQueue through which we can create objects according to the requirement. These constructor are :
- 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.
- PriorityBlockingQueue( Collection c) : This constructor creates a queue that contains the elements in a specified collection.
- PrirityBlockingQueue(int initialCapacity) : This constructor creates a queue with the specified size.
- 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.
- add(E e) : Syntax: public boolean add(E e)
- iterator() : Syntax public Iterator<E> iterator()
- poll() : Syntax public E poll()
- remove(Object o) : Syntax public boolean remove(Object o)
- size() : Syntax public int size()
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
No comments:
Post a Comment