• Java Data Structures Tutorial

Adding elements to a Queue



The add() method of the queue interface accepts an element as parameters and, adds it to the current queue.

To add elements to a queue instantiate any of the subclasses of the queue interface and add elements using the add() method.

Example

import java.util.PriorityQueue;
import java.util.Queue;

public class CreatingQueue {
   public static void main(String args[]) {

      //Create priority queue
      Queue <String>  prQueue = new PriorityQueue <String> (); 
    
      //Adding elements
      prQueue.add("JavaFX");
      prQueue.add("Java");
      prQueue.add("HBase");
      prQueue.add("Flume");
      prQueue.add("Neo4J");      
      System.out.println("Priority queue values are: " + prQueue) ; 
   }
}

Output

Priority queue values are: [Flume, HBase, Java, JavaFX, Neo4J]
Advertisements