Java PriorityQueue (priority queue, heap)

PriorityQueue

PriorityQueue is a container class in Java, inheritance follows:
java.lang.Object
java.util.AbstractCollection <E>
java.util.AbstractQueue <E>
java.util.PriorityQueue <E>

According to the document description, PriorityQueue is achieved by the heap.
Due to the need to sort, so PriorityQueue constructor Comparator interface may receive an instance, in order to compare two objects as the basis of size.

Comparator tangentially on an interface is the interface used to determine the magnitude relationship between the two numbers, to implement the interface of the interface to rewrite a
int compare (T a, T b ) Method

This method returns an int
document as described in the method returns
a negative integer, zero, or a positive integer as the first argument is less than, equal to, or greater than the second.
That returns the result is negative, zero, positive, corresponding to a is less than, equal to, greater than b.

Let's write a Person class as an object placed in PriorityQueue.

public class Person {
	private int age;
	public Person(int age)
	{
		this.age = age;
	}
	public int getAge() {
		return age;
	}
}

Next is a test class

public class Test {

	public static void main(String[] args) {
		Person p1 = new Person(1);
		Person p2 = new Person(2);
		Person p3 = new Person(3);
		Person p4 = new Person(4);
		Person p5 = new Person(5);
		PriorityQueue<Person> pq = new PriorityQueue<>((n1,n2)->n1.getAge()-n2.getAge());
		pq.offer(p3);
		pq.offer(p5);
		pq.offer(p2);
		pq.offer(p1);
		pq.offer(p4);
		Iterator<Person> it = pq.iterator();
		while(it.hasNext())
		{
			Person temp = (Person) it.next();
			System.out.println(temp.getAge());
		}
		while(!pq.isEmpty())
		{
			Person p = (Person)pq.poll();
			System.out.println(p.getAge());
		}	
	}
}

Lambda statement is used when creating PriorityQueue

PriorityQueue<Person> pq = new PriorityQueue<>((n1,n2)->n1.getAge()-n2.getAge());

Then offer methods to Person object created in the queue. Then we print queue elements in two ways.

A method Iterator

Priority queue to print heavy elements through the iterator

Iterator<Person> it = pq.iterator();
		while(it.hasNext())
		{
			Person temp = (Person) it.next();
			System.out.println(temp.getAge());
		}

The order in the queue is 35214, and 12354 print out the elements
we found printed elements are not arranged in ascending order.
Because, PriorityQueue heap is implemented, but only to ensure that the stack element is larger than the lower upper element (or less), and the top of the stack is the maximum (or minimum) elements.

The method takes two objects head

while(!pq.isEmpty())
		{
			Person p = (Person)pq.poll();
			System.out.println(p.getAge());
		}	

Since PriorityQueue inherited from the queue, so we can use the poll () pop-up teams header object, which is top of the heap objects in the pop top of the heap objects, PriorityQueue automatically for the remaining objects are processed, to ensure that is still a large root heap (small root heap ).

The object is placed in the order of 35124, and by taking the team head objects, print out the order of 12345, in line with the order from small to large.

end

In LeetCode do high frequency elements 347. [K] before, PriorityQueue to be useful, because when the stack is implemented, the solution of the problem in such "first n", the object can be placed in the stack, and then methods used to achieve the object of the top of the stack.

Released five original articles · won praise 0 · Views 57

Guess you like

Origin blog.csdn.net/weixin_41276957/article/details/104342145