Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home Answers Viewqa Java-Beginners Java collection Queue Example
Login         

View Questions and Answers by Category

Advertisements


 
Have Programming Question? Ask it here!
 
 
 


Naulej kumar
Java collection Queue Example
1 Answer(s)      2 years and 6 months ago
Posted in : Java Beginners


How can we use Queue in java collection?


Advertisement
View Answers

June 23, 2012 at 6:48 PM


import java.util.LinkedList;
import java.util.Queue;

public class MainDemo {

    public void queueExample() {

        Queue queue = new LinkedList();

        queue.add("Java");
        queue.add("DotNet");

        queue.offer("PHP");
        queue.offer("HTML");

        System.out.println("remove: " + queue.remove());
        System.out.println("element: " + queue.element());
        System.out.println("poll: " + queue.poll());
        System.out.println("peek: " + queue.peek());

    }

    public static void main(String[] args) {
        new MainDemo().queueExample();
    }
}

Output:-

remove: Java
element: DotNet
poll: DotNet
peek: PHP

Description:- In the above code, we have implemented Queue by its subclass LinkedList.We have created a method QueueExample.Using the add()and offer() method, we have added elements to LinkedList. Then, we have used remove() and poll() methods to remove elements.

element(): This method retrieves the head of the queue.

offer(E o): This inserts the specified element into the queue.

peek(): This method retrieves the head of this queue, returning null if this queue is empty.

poll(): This method retrieves and removes the head of this queue, or return null if this queue is empty.

remove(): This method retrieves and removes the head of this queue.



Related Tutorials/Questions & Answers:
Java collection Queue Example
Java collection Queue Example  How can we use Queue in java collection?   import java.util.LinkedList; import java.util.Queue; public class MainDemo { public void queueExample() { Queue queue = new
Collection : Queue Example
Collection : Queue Example In this tutorial we will describe implementation of Queue with example. Queue : A java.util.Queue interface is type... of Collection and inserts the specified element into your queue. It returns true
Advertisements
Java Queue Example
Java Queue Example  how to use queue in java ?   import... void queueExample() { Queue queue = new LinkedList(); queue.add("Java"); queue.add("DotNet"); queue.offer("PHP
Java Queue
() and peek() returns rather than removing the head of the queue. To know more about java queue go to the link:   http:/www.roseindia.net/java/example/java/util... Java Queue       A queue
queue in java
queue in java  how to performe all queue operation?   Please visit the following link: Queue Example in Java
Queue in java
Queue in java In this section we will discuss about queue in java. Queue is a interface in java.util package of java. It holds the collection of data... and remove the head of this queue. Example : A Program to implement  queue
Java collection Stack example
Java collection Stack example  How to use Stack class in java collection?   The Stack class uses the First In Last Out(FILO) mechanism... :- -1 Description:- The above example demonstrates you the Stack class in java
Queue - Java Beginners
Queue  i'm working with queue on java. since im beginners im asking for additional example programs on queue using java to enhance my knowledge. thanks so much for the help! God bless
Java Queue example
to remove elements. Following is the Java queue example: import... of the queue. It return null if the operation fails. In the following example we...Queue Interface is part of java.util package. Queue generally works on FIFO
Java Collection iterator with example
. Example of Java Collection Iterator import java.util.ArrayList; import... The Java Collection Iterator is present at the highest level interface in the Collection framework. Iterator interface has methods for traversing
stack and queue - Java Beginners
://www.roseindia.net/java/example/java/util/StackImplement.shtml http://www.roseindia.net/java/example/java/util/QueueImplement.shtml Hope...stack and queue  write two different program in java 1.) stack 2
java: implement a class queue
java: implement a class queue  Implement a class Queue which supports the following operations: boolean isEmpty() Returns true if the queue...-empty queue without deleting it. int pop() Removes the item at the front of a non
Collection : Deque Example
.style1 { font-size: medium; } Collection : Deque Example In this tutorial we will describe implementation of Queue with example. Deque... to handle different operations of deque. Example : package collection
Java Collection
Java Collection  What is the Collection interface
Java collection
Java collection  What are differences between Enumeration, ArrayList, Hashtable and Collections and Collection
Java Collection : NavigableSet Example
Java Collection : NavigableSet Example In this tutorial we are describing NavigableSet of collection Framework NavigableSet : NavigableSet interface..., boolean inclusive). Example : Here is an example of NavigableSet . package
Collection Iterate Example
Collection Iterate Example     ... of interface Iterator in Java. The description of the code is given below... the hasNext() method through the following java program. True is return
Collection : LinkedList Example
This tutorial contains description of Collection LinkedList with example
Collection : ArrayList Example
This tutorial contains description of Collection ArrayList with example
Java Programming Implement a virtual print queue
Java Programming Implement a virtual print queue  Implement a virtual print queue. A single print queue is servicing a single printer. Print... records: q,3,10 is a q type record which indicates that a print job, # 3, shall
Collection in java
Collection in java  What are the meaning of the letters E,T,K and V that come in the collection concept
Java Collection : LinkedHashSet Example
Java Collection : LinkedHashSet Example This tutorial contains description of LinkedHashSet with example. LinkedHashSet : LinkedHashSet class is defined... of HashSet. Example : package collection; import java.util.LinkedHashSet; class
Java Collection : TreeSet Example
Java Collection : TreeSet Example This tutorial contains description of  TreeSet with example. TreeSet  : TreeSet class is defined in java.util... constructor - TreeSet(), TreeSet(Collection c), TreeSet(Comparator c
Implement the Queue in Java
Implement the Queue in Java       In this section, you will learn how to implement the queue. A queue holds a collection of data or elements and follows the FIFO ( First In First
Java collection
Java collection  What is relation between map and set
Java Collection
Java Collection  What are Vector, Hashtable, LinkedList and Enumeration
Java collection
Java collection  What are differences between Enumeration, ArrayList, Hashtable and Collections
java collection
java collection  how do I use a treeset to print unique words
Collection : ArrayList Example
Collection : ArrayList Example This tutorial contains description of Collection ArrayList with example. ArrayList : For ArrayList<E> import... of elements to add. For such condition, the Collection Framework provides
Java garbage collection
Java garbage collection  What is Garbage Collection and how to call it explicitly
Java Collection API - Java Tutorials
Java Collection API Collection was added to Java with J2SE 1.2 release. Collection framework is provided in 'java.util.package'. All collections.... Example : In the below example, the method "add" of Collection
 

 

 

DMCA.com