Queue Interface and its Methods
Before we start with Queue Interface let us have a brief understanding of Collection
What is Collection?
Collection is an interface that consists of various predefined classes and interfaces with the help of which multiple data can be stored. Collection is the child class of Iterable interface,which is the topmost interface in Collection hierarchy and the relationship between Collection and Iterable is created using extends keyword.. Collection is a framework or an entity which has two packages
- java.util.Collection;
- java.util.Map;
Advantages of Collection
Collection has lot of advantages which are as follows:
- Collection can store datas of different datatypes.
- Collection is dynamic in nature so its size can be changed at runtime as per the requirement .
- Collection API consists of lot of interfaces like List, Set, and each of these interfaces have implemented classes and so the methods of these classes help in code reusability.
- Collection framework reduces the programming effort because of successful implementation of abstraction.
- Collection methods result in best implementation of program code.
Collection Hierarchy
The below diagram shows the Collection hierarchy consisting of classes and interfaces.
Now let us discuss about Queue interface and its methods
What is Queue?
Queue is an interface which stores all Collection elements. It is the child interface of Collection interface and grandchild of Iterable interface. It is present in java.utils package. The keyword used to create relationship between Queue and Collection and between Queue and Iterable is “extends”.Queue does not follow indexing and mostly all the elements are added and removed as per First In First out(FIFO) manner. In Queue the insertion of new elements happen at the end of queue and removal of elements happen in the beginning of the queue. Queue has lot of implemented classes like Priority Queue, LinkedList and ArrayDeque. Queue also has a child interface called as Deque interface. LinkedList and ArrayDeque are implemented classes of Deque interface and they are the grand child of Queue interface. Since Queue is an interface we cannot create an object of Queue directly but instead we upcast PriorityQueue,LinkedList, ArrayDeque and Deque with Queue interface in order to create Object of Queue interface.
Methods of Queue interface
Below are some of the methods of Queue interface:
Method Name | Description |
add(Object e) | This method is used to add a new element into the Queue interface. |
addAll(Collection c) | This method is used to add all elements into the Queue interface. |
clear() | It is used to clear all the elements from the Queue interface but it does not clear the Queue interface. |
contains(Object o) | This method is used to check the presence of a Collection element in the Queue. It returns ‘true’ value if the element is present in Queue and ‘false’ value if the element is not present in the Queue. |
containsAll(Collection c) | This method is used to check if the Queue contains all the collection elements. It returns ‘true’ value if all the elements are present inQueue and ‘false’ value if any one of the element is not present in the Queue. |
isEmpty() | This method checks if the given Queue is empty |
iterator() | This method is used to iterate sequentially all the Collection elements present in a Queue. |
remove(Object o) | This method is used to remove a specified collection element from the Queue. |
removeAll(Collection c) | This method removes all those Collection elements from the current Queue which are present in another Queue. |
retainAll(Collection c) | This method retains all those elements from the current Queue which are present in another Queue and removes those elements which are present in current Queue but not in another Queue and vice versa. |
size() | This method is used to find out and display the number of Collection elements present in the Queue |
toArray() | This method is used to convert all collection elements present in a Queue into an array. |
toString() | This method is used to convert all collection elements present in a Queue into a String. |
offer(Object e) | This method is used to add new elements into the Queue interface. |
poll() | This method is used to remove the topmost element of the Queue interface which is displayed in output console. |
peek() | This method is used to fetch or point out the topmost element of the Queue which is displayed in output console but does not remove that element from the Queue interface. |
element() | This method is used to fetch or point out the topmost element of the Queue which is displayed in output console but does not remove that element from the Queue interface. |
1. Let us check how add(Object e) method works in Queue interface.
Code Snippet:
package queueInterface;
import java.util.PriorityQueue;
import java.util.Queue;
public class QueueInterfaceClass {
public static void main(String[] args) {
Queue a1= new PriorityQueue();
a1.add("Hi");
a1.add("Hey");
a1.add("Hello");
a1.add("Hji");
System.out.println(a1);
}
}
Output: [Hello, Hi, Hey, Hji]
Screenshot:
2. Let us check how addAll(Collection c) works in Queue interface.
Code Snippet:
package queueInterface;
import java.util.PriorityQueue;
import java.util.Queue;
public class QueueInterfaceClass {
public static void main(String[] args) {
Queue a1= new PriorityQueue();
a1.add("Hi");
a1.add("Hey");
a1.add("Hello");
a1.add("Hji");
Queue a2= new PriorityQueue();
a2.addAll(a1);
System.out.println(a2);
}
}
Output: [Hello, Hi, Hey, Hji]
Screenshot:
3. Let us check how clear() method works in Queue interface.
Code Snippet:
package queueInterface;
import java.util.PriorityQueue;
import java.util.Queue;
public class QueueInterfaceClass {
public static void main(String[] args) {
Queue a1= new PriorityQueue();
a1.add("Hi");
a1.add("Hey");
a1.add("Hello");
a1.add("Hji");
a1.clear();
System.out.println(a1);
}
}
Output: []
Screenshot:
4. Let us check how contains(Object o) method works in Queue interface
Code Snippet:
package queueInterface;
import java.util.PriorityQueue;
import java.util.Queue;
public class QueueInterfaceClass {
public static void main(String[] args) {
Queue a1= new PriorityQueue();
a1.add("Hi");
a1.add("Hey");
a1.add("Hello");
a1.add("Hji");
System.out.println("The given queue contains Hello: "+a1.contains("Hello"));
System.out.println("The given queue contains Bye: "+a1.contains("bye"));
}
}
Output: The given queue contains Hello: true
The given queue contains Bye: false
Screenshot:
5. Let us check how containsAll(Collection c) method works in Queue interface.
Code Snippet:
package queueInterface;
import java.util.PriorityQueue;
import java.util.Queue;
public class QueueInterfaceClass {
public static void main(String[] args) {
Queue a1= new PriorityQueue();
a1.add("Hi");
a1.add("Hey");
a1.add("Hello");
a1.add("Hji");
a1.add("Kelly");
Queue a2= new PriorityQueue();
a2.add("Hi");
a2.add("Hey");
a2.add("Hello");
a2.add("Hji");
System.out.println("Does a2 contains all elements of a1:"+a2.containsAll(a1));
System.out.println("Does a1 contains all elements of a2:"+a1.containsAll(a2));
}
}
Output: Does a2 contains all elements of a1:false
Does a1 contains all elements of a2:true
Screenshot:
6. Let us check how isEmpty() method works in Queue interface.
Code Snippet:
package queueInterface;
import java.util.PriorityQueue;
import java.util.Queue;
public class QueueInterfaceClass {
public static void main(String[] args) {
Queue a1= new PriorityQueue();
a1.add("Hi");
a1.add("Hey");
a1.add("Hello");
a1.add("Hji");
a1.add("Kelly");
Queue a2= new PriorityQueue();
System.out.println("Is a1 Queue empty?: "+a1.isEmpty());
System.out.println("Is a2 Queue empty?: "+a2.isEmpty());
}
}
Output: Is a1 Queue empty?: false
Is a2 Queue empty?: true
Screenshot:
7. Let us check how iterator() method works in Queue interface.
Code Snippet:
package queueInterface;
import java.util.Iterator;
import java.util.PriorityQueue;
import java.util.Queue;
public class QueueInterfaceClass {
public static void main(String[] args) {
Queue a1= new PriorityQueue();
a1.add(12);
a1.add(16);
a1.add(14);
a1.add(15);
a1.add(11);
Iterator a2= a1.iterator();
System.out.println("The collection elements values are: ");
while(a2.hasNext())
{
System.out.println(a2.next());
}
}
}
Output: The collection elements values are:
11
12
14
16
15
Screenshot:
8. Let us check how remove(Object o) method works in Queue interface.
Code Snippet:
package queueInterface;
import java.util.PriorityQueue;
import java.util.Queue;
public class QueueInterfaceClass {
public static void main(String[] args) {
Queue a1= new PriorityQueue();
a1.add("Hi");
a1.add("Hey");
a1.add("Hello");
a1.add("Hji");
a1.add("Kelly");
a1.remove("Hello");
a1.remove("Hji");
System.out.println(a1);
}
}
Output: [Hey, Hi, Kelly]
Screenshot:
9. Let us check how removeAll(Collection c) works in Queue interface.
Code Snippet:
package queueInterface;
import java.util.PriorityQueue;
import java.util.Queue;
public class QueueInterfaceClass {
public static void main(String[] args) {
Queue a1= new PriorityQueue();
a1.add("Hi");
a1.add("Hey");
a1.add("Hello");
a1.add("Hji");
a1.add("Kelly");
Queue a2= new PriorityQueue();
a2.add("Hi");
a2.add("Hey");
a2.add("Hello");
a1.removeAll(a2);
System.out.println("Elements present in a1 after removing a2 elements from a1 are:"+a1);
}
}
Output: Elements present in a1 after removing a2 elements from a1 are:[Hji, Kelly]
Screenshot:
10. Let us check how retainAll(Collection c) works in Queue interface.
Code Snippet:
package queueInterface;
import java.util.PriorityQueue;
import java.util.Queue;
public class QueueInterfaceClass {
public static void main(String[] args) {
Queue a1= new PriorityQueue();
a1.add("Hi");
a1.add("Hey");
a1.add("Hello");
a1.add("Hji");
a1.add("Kelly");
Queue a2= new PriorityQueue();
a2.add("Hi");
a2.add("Hey");
a2.add("Hello");
a1.retainAll(a2);
System.out.println("Elements present in a1 after retaining a2 elements are:"+a1);
}
}
Output: Elements present in a1 after retaining a2 elements are:[Hello, Hi, Hey]
Screenshot:
11. Let us check how size() method works in Queue interface.
Code Snippet:
package queueInterface;
import java.util.PriorityQueue;
import java.util.Queue;
public class QueueInterfaceClass {
public static void main(String[] args) {
Queue a1= new PriorityQueue();
a1.add("Hi");
a1.add("Hey");
a1.add("Hello");
a1.add("Hji");
a1.add("Kelly");
System.out.println("The size of the collection elements present in the Queue is: "+a1.size());
}
}
Output: The size of the collection elements present in the Queue is: 5
Screenshot:
12. Let us check how toArray() method works in Queue interface.
Code Snippet:
package queueInterface;
import java.util.PriorityQueue;
import java.util.Queue;
public class QueueInterfaceClass {
public static void main(String[] args) {
Queue a1= new PriorityQueue();
a1.add("Hi");
a1.add("Hey");
a1.add("Hello");
a1.add("Hji");
a1.add("Kelly");
Object a2[]= a1.toArray();
System.out.println("The list of array elements are:");
for(int i=0;i<=a2.length-1;i++)
{
System.out.println(a2[i]);
}
}
}
Output:
The list of array elements are:
Hello
Hi
Hey
Hji
Kelly
Screenshot:
13. Let us check how toString() method works in Queue interface.
Code Snippet:
package queueInterface;
import java.util.PriorityQueue;
import java.util.Queue;
public class QueueInterfaceClass {
public static void main(String[] args) {
Queue a1= new PriorityQueue();
a1.add("Hi");
a1.add("Hey");
a1.add("Hello");
a1.add("Hji");
a1.add("Kelly");
System.out.println(a1.toString());
}
}
Output: [Hello, Hi, Hey, Hji, Kelly]
Screenshot:
14. Let us check how offer(Object e) method works in Queue interface.
Code Snippet:
package queueInterface;
import java.util.PriorityQueue;
import java.util.Queue;
public class QueueInterfaceClass {
public static void main(String[] args) {
Queue a1= new PriorityQueue();
a1.offer("Hi");
a1.offer("Hey");
a1.offer("Hello");
a1.offer("Hji");
a1.offer("Kelly");
System.out.println(a1);
}
}
Output: [Hello, Hi, Hey, Hji, Kelly]
Screenshot:
15. Let us check how poll() method works in Queue interface.
Code Snippet:
package queueInterface;
import java.util.PriorityQueue;
import java.util.Queue;
public class QueueInterfaceClass {
public static void main(String[] args) {
Queue a1= new PriorityQueue();
a1.add("Hi");
a1.add("Hey");
a1.add("Hello");
a1.add("Hji");
a1.add("Kelly");
a1.add("Looks");
System.out.println("Initial List of queue elements: "+a1);
System.out.println("Removed Element is: "+a1.poll());
System.out.println("New List of queue elements after using poll method: "+a1);
}
}
Output: Initial List of queue elements: [Hello, Hi, Hey, Hji, Kelly, Looks]
Removed Element is: Hello
New List of queue elements after using poll method: [Hey, Hi, Looks, Hji, Kelly]
Screenshot:
16. Let us check how peek() method works in Queue interface.
Code Snippet:
package queueInterface;
import java.util.PriorityQueue;
import java.util.Queue;
public class QueueInterfaceClass {
public static void main(String[] args) {
Queue a1= new PriorityQueue();
a1.add("Hi");
a1.add("Hey");
a1.add("Hello");
a1.add("Hji");
a1.add("Kelly");
a1.add("Looks");
System.out.println("Initial List of queue elements: "+a1);
System.out.println("Element on which peek method is applied: "+a1.peek());
System.out.println("New List of queue elements after using peek method: "+a1);
}
}
Output:
Initial List of queue elements: [Hello, Hi, Hey, Hji, Kelly, Looks]
Element on which peek method is applied: Hello
New List of queue elements after using peek method: [Hello, Hi, Hey, Hji, Kelly, Looks]
Screenshot:
17. Let us check how element() method works in Queue interface.
Code Snippet:
package queueInterface;
import java.util.PriorityQueue;
import java.util.Queue;
public class QueueInterfaceClass {
public static void main(String[] args) {
Queue a1= new PriorityQueue();
a1.add("Hi");
a1.add("Hey");
a1.add("Hello");
a1.add("Hji");
a1.add("Kelly");
a1.add("Looks");
System.out.println("Initial List of queue elements: "+a1);
System.out.println("Element on which element method is applied: "+a1.element());
System.out.println("New List of queue elements after using element method: "+a1);
}
}
Output: Initial List of queue elements: [Hello, Hi, Hey, Hji, Kelly, Looks]
Element on which element method is applied: Hello
New List of queue elements after using element method: [Hello, Hi, Hey, Hji, Kelly, Looks]
Screenshot:
Difference between Queue and Set
SL NO: | Queue | Set |
1 | Allows Duplicate elements | Does not allow Duplicate elements |
2 | It is used to store elements in a specific order | It is used to store unique elements |
3 | All elements are added and removed as per the First in First Out(FIFO) manner | Does not follow any specific order for addition and removal of elements. |
4 | LinkedList,Priority Queue and ArrayDeque are some of the implemented classes of Queue interface and Deque is the child interface of Queue interface | TreeSet,HashSet and LinkedHashSet are some of the implemented methods of Set. |
Conclusion:
Queue interface is used for handling all the Collection elements in a proper manner. It is necessary to understand the usage of all the implemented classes and interfaces of Queue interfaces alongwith the methods for better handling of Collection elements. With in-depth understanding of the Queue interface and its methods result in developers and testers utlilising these concepts in real life tasks. Developers could write better program codes for software applications and Testers could write better test scripts for testing the software applications due to usage of Queue interface and its methods.
Also Read :
Consult Us