Skip to main content

Queue Interface in JAVA


Queue Interface in JAVA

In this part of the tutorial set you will learn.
1) What is an interface Queue?
2) What is advantage of using Queue?
3) How to implement Queue in JAVA?
What is a interface Queue?
Queue is one of the data structure in the programming languages. Data structures are meant to store data in an organized manner. Queue can be of the first in and first out (FIFO) and they can also be of last in and first out type (LIFO). Every queue has different type of ordering properties. Here we will be discussing first in first out queue. The queue has a head and a tail. In FIFO implementation the elements are added at the head and removed from the tail.
What is the advantage of interface Queue?
Interface queues allow us to store data in more organized fashion. The methods can let us retrieve and insert data easily. Interface queues can be used for holding data variables from different methods at a same time. In operating systems and micro controller programming the interface queue is used for managing interrupts and holding the values of the program counter.
How to implement interface queue in JAVA?
As you can see in the code below, first we have imported the ‘import java.util.LinkedList;’ and ‘import java.util.Queue;’ libraries, then we have declared a queue as ‘myQueue’, it must be noted here that our queue is made by link list. The ‘add’ method takes in integer or a string according to the data type of the queue and add it into the queue with which it is called. The ‘size’ function takes in the queue and return its size by counting the number of elements inside it. ‘peek ’ command is used to just retrieve the element at the head of the queue. ‘remove’ command removes the element at the head and there is decrease of size by one upon removal. Remove command can also throw exception. ‘element’ command is also used to retrieve the data at the head of the queue but it can also throw exception. ‘poll’ command also removes the element present at the head of the queue but it does not uses exceptions.
In this code below I have demonstrated how the various commands of interface queue are used to retrieve and insert data, how the size is increased and decreased dynamically upon insertion and removal of elements.

  1. Code:
  2. import java.util.LinkedList;
  3. import java.util.Queue;
  4.  
  5.  
  6. public class UsingJAVA {
  7. public static void main(String[] args) {
  8.  
  9. Queue<String> myQueue = new LinkedList<String>();
  10. myQueue.add("I");
  11. myQueue.add("am");
  12. myQueue.add("Learning");
  13. myQueue.add("JAVA.");
  14. myQueue.add("It");
  15. myQueue.add("is");
  16. myQueue.add("Interesting.");
  17. System.out.println("The size of the queue is :");
  18. System.out.print(myQueue.size());
  19. System.out.println();
  20.  
  21. String a;
  22. String b;
  23. System.out.println("The result after peek and element is:");
  24. a=myQueue.peek(); //peek retrieves but does not remove
  25. b=myQueue.element(); //remove and element throw exceptions
  26.  
  27.  
  28. System.out.print(a);
  29. System.out.println();
  30. System.out.print(b);
  31. System.out.println();
  32. System.out.println("Using 'poll', which removes the items");
  33. System.out.print(myQueue.poll()); //poll removes
  34. System.out.println();
  35. System.out.print(myQueue.poll());
  36. System.out.println();
  37. System.out.println("'Remove' command removes an element which is:");
  38. System.out.println(myQueue.remove());
  39. System.out.println("Emptying the whole queue using clear command");
  40. myQueue.clear();
  41. System.out.println();
  42. System.out.println("Size of the queue after clearance is:");
  43. System.out.print(myQueue.size());
  44.  
  45. }
  46. }

Screen Shot
Screen Shot

Tags

Note: Due to the size or complexity of this submission, the author has submitted it as a .zip file to shorten your download time. After downloading it, you will need a program like Winzip to decompress it.

Virus note: All files are scanned once-a-day by SourceCodester.com for viruses, but new viruses come out every day, so no prevention program can catch 100% of them.

FOR YOUR OWN SAFETY, PLEASE:

1. Re-scan downloaded files using your personal virus checker before using it.
2. NEVER, EVER run compiled files (.exe's, .ocx's, .dll's etc.)--only run source code.

Add new comment

CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.