Java Queue Interface

  • If y'all await at collection hierarchy, Queue extends collection interface.
  • Main role of collection is to concord the elements prior to processing.
  • Queue interface provides roughly additional operations similar insertion, inspection as well as extraction equally good collection interface operations.
  • All these iii operations be inwards 2 dissimilar forms. One render particular value(null or faux depending on the operation) if functioning fails else throws an exception if functioning fails.
  • Queue follows FIFO(first-in-first-out) as well as then it volition insert chemical gene at the terminate of the queue when y'all insert novel chemical gene as well as chemical gene volition move removed from the offset of the queue when y'all withdraw element.
  • As Queue interface is sub type of collection interface, all methods of collection interface are also available in Queue interface.
Queue Interface hierarchy


 Basic methods of Queue interface
Few of the basic Queue interface methods are equally below.
  • boolean add(E e) : It volition insert an chemical gene inwards queue if infinite is available inwards queue. Else it volition render IllegalStateException.
  • E element() : It volition render head(first element) of queue if queue inwards non empty. It volition render NoSuchElementException exception if Queue is empty.
  • boolean offer(E e) : It volition insert chemical gene inwards queue if infinite is available inwards queue.
  • E peek() : It volition render head(first element) of queue if queue inwards non empty. Else it volition render null.
  • E poll() : It volition yell back as well as withdraw head(first element) of the element. Return zilch if queue is empty.
  • E remove() : It volition yell back as well as withdraw head(first element) of the element. It volition render NoSuchElementException exception if Queue is empty.
I accept prepared illustration on basic Queue interface methods to present demo how they works.

Queue Interface Example
package JAVAExamples;  import java.util.LinkedList; import java.util.Queue;  world course of didactics QueueExample {   world static void main(String[] args) {   //Creating queue.   Queue q = novel LinkedList();      //Inserting elements inwards queue.   q.add("three");   q.add("two");   q.add("one");     q.add("four");   q.add("six");   q.add("seven");   q.add("five");      //Print queue elements.   System.out.println("Queue elements are : "+q);      //Removing initiative of all inserted chemical gene from queue using withdraw method.   q.remove();   System.out.println("Queue elements afterward chemical gene removal using withdraw method : "+q);      //retrieve caput of queue (first element) using chemical gene method.   System.out.println("Now initiative of all chemical gene inwards queue is : "+q.element());      //Insert chemical gene inwards queue using offering method.    q.offer("eight");   System.out.println("Queue elements afterward inserting chemical gene using offering method : "+q);      //Removing specific chemical gene from queue.   q.remove("six");   System.out.println("Queue elements afterward removing half dozen chemical gene : "+q);    //Removing caput of queue (first element) using poll method.   q.poll();   System.out.println("Queue elements afterward removing chemical gene using poll method : "+q);      //retrieve caput of queue (first element) using peek method.   System.out.println("Now initiative of all chemical gene inwards queue is : "+q.peek());    } }

Output :
Queue elements are : [three, two, one, four, six, seven, five] Queue elements afterward chemical gene removal using withdraw method : [two, one, four, six, seven, five] Now initiative of all chemical gene inwards queue is : 2 Queue elements afterward inserting chemical gene using offering method : [two, one, four, six, seven, five, eight] Queue elements afterward removing half dozen chemical gene : [two, one, four, seven, five, eight] Queue elements afterward removing chemical gene using poll method : [one, four, seven, five, eight] Now initiative of all chemical gene inwards queue is : one


More interesting articles here :Generation Enggelmundus Internet Marketing Tool here :Zeageat IM http://www.software-testing-tutorials-automation.com/
Post a Comment (0)
Previous Post Next Post