Friday, 30 December 2016

How to create PriorityQueue in Java?

package com.kt4study.corejava.collection;

import java.util.PriorityQueue;

/**
 @author kt4study.blogspot.co.uk
 
 */

public class PriorityQueueExample {

  public PriorityQueue<Integer> getPriorityQueue() {
    // Create An Instance Of PriorityQueue
    PriorityQueue<Integer> priorityQueue = new PriorityQueue<Integer>();
    // Add Integer Object in PriorityQueue.
    priorityQueue.add(5);
    priorityQueue.add(10);

    return priorityQueue;
  }

  public static void main(String[] args) {
    PriorityQueueExample priorityQueueExample = new PriorityQueueExample();
    // Print all the elements From PriorityQueue
    for (Integer element : priorityQueueExample.getPriorityQueue()) {
      System.out.println("Element from PriorityQueue :: " + element);
    }
  }
}
Output

Element from PriorityQueue :: 5
Element from PriorityQueue :: 10

No comments:

Post a Comment