Java - PriorityQueue example

Posted on October 25, 2016


A PriorityQueue is an unbound priority queue based on priorty heap. The orders of elements in the PriorityQueue depend on the type of constructor used. The PriorityQueue are ordered by default in natural order. We can provide the comparator at time of instantiation of priority queue.

The basic operations of PrioryQueue are

  • offer() and add() - Inserts the specified element into priority queue.
  • poll() and remove() - Retrieves and removes the head of priority queue.
  • peek() and element() - Retrieves, but does not remove, the head of priority queue.

Methods add() ,remove()  and element()  throw exception if any condition fails.

Methods offer(), poll() and peek()  return special value (either null or false, depending on the operation) if any condition fails. 

 

In this section, we will show you an example of priority queue of persons. 

Person.java 

package com.boraji.tutorial;

/**
 * @author imssbora
 *
 */
public class Person{
	private int id;
	private String name;
	private int age;
	
	public Person(int id,String name,int age) {
		this.setId(id);
		this.setName(name);
		this.setAge(age);
	}

	public int getAge() {
		return age;
	}

	public void setAge(int age) {
		this.age = age;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}
	
	@Override
	public String toString() {
		return "("+id+","+name+","+age+")";
	}
}

 

PersonComparator.java  - This class will determine the priority of queue based on age of person.

package com.boraji.tutorial;

import java.util.Comparator;

/**
 * @author imssbora
 *
 */
public class PersonComparator implements Comparator<Person>{
	@Override
	public int compare(Person o1, Person o2) {
		return (o2.getAge()-o1.getAge());
	}
}

 

PriorityQueueExample.java

package com.boraji.tutorial;

import java.util.PriorityQueue;
import java.util.Queue;

/**
 * @author imssbora
 *
 */
public class PriorityQueueExample {
	public static void main(String[] args) {
		Queue<Person> queue=new PriorityQueue<>(new PersonComparator());
		
		/*Add elements to queue*/
		queue.offer(new Person(1,"Sunil",25));
		queue.offer(new Person(2,"John",20));
		queue.offer(new Person(3,"Jack",30));
		queue.offer(new Person(4,"Mack",40));
		queue.offer(new Person(5,"Tom",15));
		
		
		System.out.println("Elements in queue: "+queue);
		
		/*Remove element from the queue*/
		Person personEle=queue.poll();
		System.out.println("Removed element: "+personEle.toString());
		System.out.println("Elements in queue after poll: "+queue);
		
		/*Get first element from the queue*/
		Person person=queue.peek();
		System.out.println("Returned element: "+person.toString());
		System.out.println("Elements in queue after peek: "+queue);
		
	}
}

Output

Elements in queue: [(4,Mack,40), (3,Jack,30), (1,Sunil,25), (2,John,20), (5,Tom,15)]
Removed element: (4,Mack,40)
Elements in queue after poll: [(3,Jack,30), (2,John,20), (1,Sunil,25), (5,Tom,15)]
Returned element: (3,Jack,30)
Elements in queue after peek: [(3,Jack,30), (2,John,20), (1,Sunil,25), (5,Tom,15)]