instagram

PriorityQueue java example

PriorityQueue is perform sorting in ACE order on data.

Input : { 3, 5, 2, 7, 9, 3, 7, 2, 1, 4, 6 }
Output : 1 2 2 3 3 4 5 6 7 7 9

package com.omt.learn.algo;

import java.util.PriorityQueue;

public class PriorityQueueExample {

	public static void main(String[] args) {

		int[] a = { 3, 5, 2, 7, 9, 3, 7, 2, 1, 4, 6 };
		printFromPriorityQueue(a);

	}

	public static void printFromPriorityQueue(int[] a) {

		PriorityQueue<Integer> pq = new PriorityQueue<>();

		for (int i : a) {
			pq.add(i);
		}

		while (pq.size() > 0) {
			System.out.print(pq.poll() + " ");
		}

	}

}
Share