Skip to content

Instantly share code, notes, and snippets.

@gssachdeva
Created August 24, 2021 09:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gssachdeva/bb27fa21d6a090dd13d9780384816c01 to your computer and use it in GitHub Desktop.
Save gssachdeva/bb27fa21d6a090dd13d9780384816c01 to your computer and use it in GitHub Desktop.
package queue;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Iterator;
public class MyQueue {
public static void main(String[] args) {
System.out.println("*** Fun with Queue ***");
System.out.println();
//Use the generic <Queue> Interface type to perform operations instead
//of exact implementation class <LinkedList>
Queue<String> queue = new LinkedList<String>();
//Insert five string elements using <add> operation
queue.add("first element");
queue.add("second element");
queue.add("third element");
queue.offer("fourth element");
queue.offer("fifth element");
System.out.println("Queue contents:");
System.out.println(queue);
System.out.println();
//Check the size of queue
int size = queue.size();
System.out.println("Size of the queue = " + size);
System.out.println();
//Check if specified element is present in queue
boolean containsSixthElement = queue.contains("sixth element");
boolean containsThirdElement = queue.contains("third element");
//This element is not present inside the queue
System.out.print("Does the Queue contain \"sixth element\" --> ");
if (containsSixthElement == true) {
System.out.println("Yes");
}
else {
System.out.println("No");
}
//This element is present inside the queue
System.out.print("Does the Queue contain \"third element\" --> ");
if (containsThirdElement == true) {
System.out.println("Yes");
}
else {
System.out.println("No");
}
System.out.println();
//Iterate through the queue
Iterator<String> iterator = queue.iterator();
int elementCount = 0;
System.out.println("Iterating through the Queue:");
while(iterator.hasNext() == true) {
String currentElement = iterator.next();
System.out.println("Position = " + elementCount++ +
" Element = \"" + currentElement + "\"");
}
//Examine the queue using <peek> and <element> operation
System.out.println();
System.out.println("Examine head element using <peek> operation:");
String headElement = queue.peek();
System.out.println("<peek> operation : Head Element --> " +
headElement);
System.out.println("Examine head element using <element> operation:");
headElement = queue.element();
System.out.println("<element> operation : Head Element --> " +
headElement);
//Remove the current head element using <poll> operation
System.out.println();
System.out.println("Removing Elements:");
String firstElement = queue.poll();
System.out.println("First Element to be extracted (poll) = \"" +
firstElement + "\"");
//Remove the current head element using <remove> operation
String secondElement = queue.remove();
System.out.println("Second Element to be extracted (remove) = \"" +
secondElement + "\"");
//Queue size should now reflect the lesser number of elements
System.out.println();
System.out.println("Checking Queue size after extraction of elements:");
int currentSize = queue.size();
System.out.println("Original Queue Size = " + size);
System.out.println("Current Queue Size = " + currentSize);
//Remove head element
System.out.println();
System.out.println("Removing head element:");
String removedElement = queue.remove();
System.out.println("Removed Element = " + removedElement);
System.out.println("Checking Queue size after removal of element:");
int reducedSize = queue.size();
System.out.println("Reduced Queue Size = " + reducedSize);
//Remove all elements from queue
queue.clear();
currentSize = queue.size();
System.out.println();
System.out.println("Queue Size after removing all elements = " +
currentSize);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment