Thursday, February 25, 2016

Java Queue Interface

| Thursday, February 25, 2016
  • If you lot hold back at collection hierarchy, Queue extends collection interface.
  • Main operate of collection is to concur the elements prior to processing.
  • Queue interface provides to a greater extent than or less additional operations similar insertion, inspection too extraction too collection interface operations.
  • All these 3 operations be inwards 2 dissimilar forms. One render exceptional 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) too then it volition insert chemical component subdivision at the terminate of the queue when you lot insert novel chemical component subdivision too chemical component subdivision volition endure removed from the kickoff of the queue when you lot withdraw element.
  • As Queue interface is sub type of collection interface, all methods of collection interface are also available in Queue interface.
Main operate of collection is to concur the elements prior to processing Java Queue Interface


 Basic methods of Queue interface
Few of the basic Queue interface methods are every bit below.
  • boolean add(E e) : It volition insert an chemical component subdivision 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 component subdivision 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 think too withdraw head(first element) of the element. Return cipher if queue is empty.
  • E remove() : It volition think too withdraw head(first element) of the element. It volition render NoSuchElementException exception if Queue is empty.
I accept prepared instance 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 teaching 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 firstly inserted chemical component subdivision from queue using withdraw method.   q.remove();   System.out.println("Queue elements afterward chemical component subdivision removal using withdraw method : "+q);      //retrieve caput of queue (first element) using chemical component subdivision method.   System.out.println("Now firstly chemical component subdivision inwards queue is : "+q.element());      //Insert chemical component subdivision inwards queue using offering method.    q.offer("eight");   System.out.println("Queue elements afterward inserting chemical component subdivision using offering method : "+q);      //Removing specific chemical component subdivision from queue.   q.remove("six");   System.out.println("Queue elements afterward removing half dozen chemical component subdivision : "+q);    //Removing caput of queue (first element) using poll method.   q.poll();   System.out.println("Queue elements afterward removing chemical component subdivision using poll method : "+q);      //retrieve caput of queue (first element) using peek method.   System.out.println("Now firstly chemical component subdivision inwards queue is : "+q.peek());    } }

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

Related Posts