June 21

0  comments

What is Queue interface and what are the characteristics of Queue in JAVA?

Queue in JAVA Collection framework is an interface, which extends Collection interface. It is part of java.util package. It is used to store objects to be processed in FIFO order (First In First Out).

Since Queue is an interface, needs a concrete class for the declaration and the most common classes are the PriorityQueue and LinkedList.

How to add element to a Queue?

There are 2 methods we can use to add element to a Queue, add() and offer(). The methods differ in how the behave if the Queue is full, so no more elements can be added. The add() method throws an exception in that case, whereas the offer() method just returns false.

Example –

import java.util.*;

public class Main {

public static void main(String[] args) {
Queue<String> queue = new LinkedList<>();

queue.add("element 1");
queue.offer("element 2");

}
}

How do we remove element from a Queue?

There are 2 methods which can be used to remove elements from a Queue. They are remove() and poll().

Both removes the first element of the Queue and returns the removed element. If the Queue is empty, remove() throws an exception while poll() returns null.

Example

public class Main {

public static void main(String[] args) {
Queue<String> queue = new LinkedList<>();

queue.add("element 1");
queue.offer("element 2");
String e1 = queue.remove();
String e2 = queue.poll();

System.out.println(e1);
System.out.println(e2);

}
}


Output

element 1
element 2

Explanation

The remove() method removes the first element which is ‘element 1’ and poll() method removes the second element which is ‘element 2’.

Observe that FIFO rule is followed here.

How do we find head element of a queue?

We can find the head element of the Queue using element() or peek() methods, without removing the elements.

 If the queue is non-empty, both returns the elements. Otherwise, element() throws exception and peek() returns null.

Example

import java.util.*;

public class Main {

public static void main(String[] args) {
Queue<String> queue = new LinkedList<>();

// Add elements to the queue
queue.add("element 1");
queue.offer("element 2");
// Find the head
String e1 = queue.element();
// Remove the head
queue.poll();
// Find the head
String e2 = queue.peek();
// Remove the head
queue.poll();
// Find the head for empty queue
String e3 = queue.peek();

System.out.println(e1);
System.out.println(e2);
System.out.println(e3);

}
}



Output

element 1
element 2
null

How to iterate all elements of a Queue?

We can use Iterator to iterate all elements of a Queue. One important thing to note is, if the implementation of Queue is LinkedList, after iterating, the elements will be returned based on insertion order (default behavior of LinkedList class) but if PriorityQueue implementation is used, the output will be in sorted order.

Let’s see the difference using 2 examples.

Example 1 – LinkedList

import java.util.*;

public class Main {

public static void main(String[] args) {
Queue<String> queue = new LinkedList<>();

// Add elements to the queue
queue.offer("element 4");
queue.offer("element 5");
queue.add("element 1");
queue.offer("element 2");
queue.offer("element 3");

Iterator<String> itr = queue.iterator();
while(itr.hasNext())
{
System.out.println(itr.next());
}


}
}


Output (Maintains the insertion order)

element 4
element 5
element 1
element 2
element 3

Example 2 – PriorityQueue

import java.util.*;

public class Main {

public static void main(String[] args) {
Queue<String> queue = new PriorityQueue<>();

// Add elements to the queue
queue.offer("element 4");
queue.offer("element 5");
queue.add("element 1");
queue.offer("element 2");
queue.offer("element 3");

Iterator<String> itr = queue.iterator();
while(itr.hasNext())
{
System.out.println(itr.next());
}


}
}


Output (Elements are returned in sorted way)

element 1
element 2
element 4
element 5
element 3


Tags


You may also like

Groups attribute in TestNG 

Groups attribute in TestNG 
Leave a Reply

Your email address will not be published. Required fields are marked

{"email":"Email address invalid","url":"Website address invalid","required":"Required field missing"}