package com.queuesample;
import java.util.*;
public class QueueDemo {
public static void main(String[] args) {
System.out.println( "Queue in Java" );
System.out.println("-----------------------" );
System.out.println("Adding items to the Queue" );
// Creating queue would require you to create instance of LinkedList
// and assign
// it to Queue
// Object. You cannot create an instance of Queue as it is abstract
Queue queue = new LinkedList();
// you add elements to queue using add method
queue.add("Java");
queue.add(".NET");
queue.add("Javascript");
queue.add("HTML5");
queue.add("Hadoop");
System.out.println( "Items in the queue..." + queue );
System.out.println("Before removing "+queue.element());
// You remove element from the queue using .remove method
// This would remove the first element added to the queue, here Java
System.out.println("remove element: " + queue.remove() );
// .element() returns the current element in the queue, here when "java"
// is removed
// the next most top element is .NET, so .NET would be printed.
System.out.println("retrieving element: " + queue.element() );
// .poll() method retrieves and removes the head of this queue
// or return null if this queue is empty. Here .NET would be printed and
// then would
// be removed
// from the queue
System.out.println("remove and retrieve element, null if empty: "
+ queue.poll() );
System.out.println("After the polling method "+queue.element());
// .peek() just returns the current element in the queue, null if empty
// Here it will print Javascript as .NET is removed above
System.out.println("retrieve element, null is empty " + queue.peek()
);
PriorityQueue priorityQueue = (PriorityQueue)queue;
}
}
import java.util.*;
public class QueueDemo {
public static void main(String[] args) {
System.out.println( "Queue in Java" );
System.out.println("-----------------------" );
System.out.println("Adding items to the Queue" );
// Creating queue would require you to create instance of LinkedList
// and assign
// it to Queue
// Object. You cannot create an instance of Queue as it is abstract
Queue queue = new LinkedList();
// you add elements to queue using add method
queue.add("Java");
queue.add(".NET");
queue.add("Javascript");
queue.add("HTML5");
queue.add("Hadoop");
System.out.println( "Items in the queue..." + queue );
System.out.println("Before removing "+queue.element());
// You remove element from the queue using .remove method
// This would remove the first element added to the queue, here Java
System.out.println("remove element: " + queue.remove() );
// .element() returns the current element in the queue, here when "java"
// is removed
// the next most top element is .NET, so .NET would be printed.
System.out.println("retrieving element: " + queue.element() );
// .poll() method retrieves and removes the head of this queue
// or return null if this queue is empty. Here .NET would be printed and
// then would
// be removed
// from the queue
System.out.println("remove and retrieve element, null if empty: "
+ queue.poll() );
System.out.println("After the polling method "+queue.element());
// .peek() just returns the current element in the queue, null if empty
// Here it will print Javascript as .NET is removed above
System.out.println("retrieve element, null is empty " + queue.peek()
);
PriorityQueue priorityQueue = (PriorityQueue)queue;
}
}
No comments:
Post a Comment