Site icon Narayana Tutorial

Queue 1.5 version

Queue

*image*

Methods of Queue interface:-

  1. Boolean offer (object o )
    • For adding an object to the Queue.
  2. Object Peek()
    • Return head element of the Queue . If the Queue is empty this method returns null.
  3. Object elements().
    • Returns head element of the Queue. If Queue is empty, this method raises RuntimeException saying NoSuchElementException.
  4. Object remove()
    • Remove & returns head element of the Queue. If the Queue is empty, this method returns null.
  5. Object remove()
    • Remove & returns head element of the Queue. If Queue is empty, this method raises NoSuchElementException.

Priority Queue

Constructor

PriorityQueue q = new PrioryQueue()

  1. Creates an empty priorityQueue with default initial capacity 11 & sorting order in natural sorting order.
  2. PriorityQueue q = new PriorityQueue(int initialcapacity);
  3. PriorityQueue q = new PriorityQueue (int initialcapacity, Comparator c);
  4. PriorityQueue q = new PriorityQueue (SortedSet s). // some platforms may not provide the support for PriorityQueues properly.

PriorityQueueDemo1.java

package com.narayanatutorial.collections.queue;

import java.util.NoSuchElementException;
import java.util.PriorityQueue;

public class PriorityQueueDemo1 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		PriorityQueue<Integer> q = new PriorityQueue<Integer>();
		System.out.println(q.peek());// null		
		try {
			System.out.println(q.element()); // NoSuchElementException	
		}catch(NoSuchElementException e) {
			System.out.println("No Such Element Exception.."+e);
		}
		for (int i = 0; i <= 10; i++) {
			q.offer(i);
		}
		System.out.println("PriorityQueue:"+q);// 10,1,2,3,....10
		System.out.println("poll:"+q.poll()); // 0
		System.out.println("PriorityQueue:"+q); // [1,2,3,4.....10]
	}

}

Output

null
No Such Element Exception..java.util.NoSuchElementException
PriorityQueue:[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
poll:0
PriorityQueue:[1, 3, 2, 7, 4, 5, 6, 10, 8, 9]

Mycomparator.java

package com.narayanatutorial.collections.queue;

import java.util.Comparator;

public class Mycomparator implements Comparator<Object> {

	@Override
	public int compare(Object obj1, Object obj2) {
		// TODO Auto-generated method stub
		String s1 = (String) obj1;
		String s2 = (String) obj2;
		return s2.compareTo(s1);

	}

}

PriorityQueueDemo2.java

package com.narayanatutorial.collections.queue;

import java.util.PriorityQueue;



public class PriorityQueueDemo2 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		 PriorityQueue<String> q =new PriorityQueue<String>(new Mycomparator());
         q.offer("A");
         q.offer("B");
         q.offer("Z");
         q.offer("L");
       System.out.println("customize sorting order:"+q); // [ZLBA] 
       
       
       PriorityQueue<String> q1 =new PriorityQueue<String>(new Mycomparator().reversed());
       q1.offer("A");
       q1.offer("B");
       q1.offer("Z");
       q1.offer("L");
     System.out.println("customize sorting order with reversed method of comparator:"+q1); // [ZLBA] 
       
       
	}

}

Output

customize sorting order:[Z, L, B, A]
customize sorting order with reversed method of comparator:[A, B, Z, L]

Reference Links

https://docs.oracle.com/javase/6/docs/api/