Priorityqueue
an unbounded priority queue based on the priority heap. The elements of the priority queue are sorted in their natural order, or sorted according to the Comparator provided when the queue is constructed, depending on the method of construction used. The priority queue does not allow null elements to be used. Priority queues that rely on natural order are also not allowed to insert objects that are not comparable (this may cause classcastexception).
Package com.enterise.always.priorityqueue;
Import Java.util.PriorityQueue;
Import Java.util.Random;
public class Mainaction {public
static void Main (string[] args) {
priorityqueue<student> studentqueue = NE W priorityqueue<student> ();
for (int i = 0; i < i++) {
System.out.println ("I--->>" +i);
Studentqueue.add (New Student (New Random (). nextint));
int size = Studentqueue.size ();
for (int j = 0; J < size; J +) {
Student s = studentqueue.poll ();
SYSTEM.OUT.PRINTLN ("s--->" +s.getid ());}}
Introduction to the Add Method:
inserts the specified element into this priority queue.
ClassCastException-If the specified element cannot be compared to the current element in this priority queue according to the order of the priority queue.
NullPointerException-If the specified element is null.
so in the Add method, the elements to be added are compared, and if you have a higher priority, then it's in the front, and vice versa.
if the added element is inconsistent, then it cannot be compared, so the element to be added must implement the comparable interface, or when constructing the priorityqueue, it is passed into a specified comparer for comparison.
The source code realization is:
/**
* Inserts the specified element into this priority queue.
*
* @return {@code true} (as specified by {@link collection#add})
* @throws classcastexception if the specified El Ement cannot is
* compared with elements currently in this priority queue * According to the priority Queue ' s ordering
* @throws NullPointerException If the specified element is null
*
/public boolean add (E) {return offer
(e);
}
Public Boolean offer (E e) {
if (E = = null)
throw new NullPointerException ();
modcount++;
int i = size;
if (i >= queue.length)
Grow (i + 1);
size = i + 1;
if (i = = 0)//is just beginning to add without comparison.
queue[0] = e;
else
Siftup (i, E);//is compared against the specified comparer. return
true;
}
private void Siftup (int k, E x) {///first to determine if there is a comparer in the construct.
if (comparator!= null)
Siftupusingcomparator (k, x);
else
siftupcomparable (k, x);
}
private void Siftupusingcomparator (int k, E x) {while
(k > 0) {
int parent = (k-1) >>> 1;
Object e = queue[parent];
if (Comparator.compare (x, (e) e) >= 0) break
;
Queue[k] = e;
K = parent;
}
QUEUE[K] = x;
}
private void siftupcomparable (int k, E x) {
comparable<? super e> key = (comparable<? super e>) x;
while (k > 0) {
int parent = (k-1) >>> 1;
Object e = queue[parent];
if (Key.compareto ((e) e) >= 0) break
;
Queue[k] = e;
K = parent;
}
QUEUE[K] = key;
}
So the queue that implements the priority is internally aligned with the specified constructor.
Poll method:
gets and removes the header of this queue, and returns null if the queue is empty.
Public E Poll () {
if (size = 0) return
null;
int s =--size;
modcount++;
E result = (e) queue[0];
E x = (e) queue[s];
Queue[s] = null;
if (S!= 0)
siftdown (0, x);
return result;
}
This object is thread-unsafe.
linkedblockingqueue (thread safe)