Queue
- Eclipse: Oxygen
- Java: 1.8
The queue is a data structure that First in First Out(FIFO) structure. This is based on the real-life concept of the queue or a line of people. Therefore, this can be very useful in several ways, such as to find the level order traversal in a tree, etc. Therefore, the basic operation of this is shown here.
In the following example, we will create the LinkedBlockingQueue. LinkedBlockingQueue is of dynamic size. It’s not required to provide size while creating LinkedBlockingQueue.
add() method is used to add data at the end of the queue, and we can iterate queue elements using enhanced for loop.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
public class App { public static void main(String[] args) { Queue<Integer> queue = new LinkedBlockingQueue<>(); queue.add(1); queue.add(2); queue.add(3); queue.add(4); queue.add(5); for(Integer element: queue){ System.out.println(element); } } } |
Output
1 2 3 4 5 6 7 |
1 2 3 4 |
In the following example, we will create ArrayBlockingQueue.
Like Arrays, ArrayBlockingQueue is of fixed size. Size of ArrayBlockingQueue should be provided as arguments while creating a queue. add() method is used to add data at the end of the queue.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
public class App { public static void main(String[] args) { Queue<Integer> queue = new ArrayBlockingQueue<>(6); queue.add(1); queue.add(2); queue.add(3); queue.add(4); queue.add(5); queue.add(6); for(Integer element: queue){ System.out.println(element); } } } |
Output:
1
2
3
4
5
6
When the size of the queue is exceeded, it throws IllegalStateException.
remove()Â method removes the first element of the queue. If we try to remove an element from an empty queue, it throws NoSuchElementException.
element() method returns the first element of the queue. . If we try to examine any empty queue, it throws NoSuchElementException.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
public class App { public static void main(String[] args) { Queue<Integer> queue = new ArrayBlockingQueue<>(6); queue.add(1); queue.add(2); queue.add(3); queue.add(4); queue.add(5); queue.add(6); System.out.println("First element in Queue: "+queue.element()); } } |
Output
1 |
1 |
offer() is a special method used for adding data into a queue, which returns true if data is successfully added into a queue and false if data is not added.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
public class App { public static void main(String[] args) { Queue<Integer> queue = new ArrayBlockingQueue<>(6); System.out.println(queue.offer(1)); queue.offer(2); queue.offer(3); queue.offer(4); queue.offer(5); queue.offer(6); System.out.println(queue.offer(7)); for(Integer element: queue){ System.out.println(element); } } } |
Output
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
True // 1 is added false //7 is not added 1 2 3 4 5 6 |
poll()Â is a special method that removes the first element of the queue and returns the element which is removed from the queue. If we try to poll any element from an empty queue, it returns null.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
public class App { public static void main(String[] args) { Queue<Integer> queue = new ArrayBlockingQueue<>(6); System.out.println(queue.poll()); queue.offer(1); queue.offer(2); queue.offer(3); queue.offer(4); queue.offer(5); queue.offer(6); for(Integer element: queue){ System.out.println(element); } System.out.println("removed element from Queue: "+queue.poll()); } } |
Output
null
1
2
3
4
5
6
removed element from Queue: 1
peek() is also a special method that checks for the first element of the queue. It returns null if implemented on an empty queue.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
public class App { public static void main(String[] args) { Queue<Integer> queue = new ArrayBlockingQueue<>(6); System.out.println(queue.peek()); queue.offer(1); queue.offer(2); queue.offer(3); queue.offer(4); queue.offer(5); queue.offer(6); queue.offer(7); for(Integer element: queue){ System.out.println(element); } System.out.println("********************"); System.out.println(queue.peek()); } } |
Output
null
1
2
3
4
5
6
********************
1
Dequeue
This means a DOUBLE ENDED QUEUE. The queue is seen by us previously. The double-ended queue means insertion and deletion can take place from both ends. Therefore, the basic operations of this data structure are shown here.
In the following example, all the Queue methods can be used with Deque, but Deque has its own methods which allow to add and remove elements at the beginning and end of Deque.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
public class App { public static void main(String[] args) { Deque<Integer> deque = new LinkedBlockingDeque<>(); deque.offer(1); deque.offer(2); deque.offer(3); deque.offer(4); deque.offer(5); deque.offer(6); deque.offer(7); for(Integer element: deque){ System.out.println(element); } System.out.println("********************"); deque.addFirst(0); deque.removeLast(); for(Integer element: deque){ System.out.println(element); } } } |
Output
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
1 2 3 4 5 6 7 ******************** 0 1 2 3 4 5 6 |
Follow the table where special methods are mentioned:
Contributed by Poonam Tomar