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



Home Answers Viewqa Java-Beginners Java Queue Example
Login         

View Questions and Answers by Category

Advertisements


 
Have Programming Question? Ask it here!
 
 
 


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


how to use queue in java ?


Advertisement
View Answers

July 4, 2012 at 7:11 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 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 collection Queue Example
Java collection Queue Example  How can we use Queue in java... class MainDemo { public void queueExample() { Queue queue = new LinkedList(); queue.add("Java"); queue.add("DotNet
Advertisements
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... and remove the head of this queue. Example : A Program to implement  queue... :"+qe.size()); } } Description : In the above example we have implemented queue
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
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 NoSuchElementException in place of null if queue is empty. Example
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
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
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
Implement the Queue in Java
Implement the Queue in Java       In this section, you will learn how to implement the queue. A queue... program implements a queue. It takes all elements as input by user. These values
JMS QUEUE
JMS QUEUE  how to create queue and queueconnectionfactory in websphere application server
Stack and Queue
. For example, once created, the user can simply ask the configuration object...? ); In the above example, the config object is constructed and its contents are expressed... functions will behave differently depending on whether your class is a Queue
{create stack with queue}
{create stack with queue}  how can create queue that accept all data type ?? meanse that this array is used accept all type for example(int ,char,String,float).?? please help me ....which output is : 1- to create queue 2-Equeue
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
how to create a queue - JMS
how to create a queue  Can u please tell me how to create q queue in JMS and also tell me how to write a program to Send a Static Message for JMS Queue ....please i need it urgently
AJAX Queue Class
AJAX Queue Class       This is a JavaScript class that makes using AJAX really, REALLY simple and robust. It supports every browser capable of AJAX (including IE5/Win98) and manages
JavaScript array queue
="#ddcdff"> <h2> JavaScript array Queue Example </h2>... JavaScript array queue       In the JavaScript array we can implement the Queue by using the push
Queue implementation using array.
Description: In this tutorial you will see how to implement queue using array and queue insert & delete operations. Code: #include <stdio.h> #define MAX 5 #include <stdlib.h> void insert(int queue[], int *rear
The queue, dequeue & clearQueue effect of jQuery
The queue, dequeue & clearQueue effect of jQuery In this tutorial, we will discuss about the queue, dequeue & clearQueue effects of jQuery. Given below the functionality of the queue, dequeue & clearQueue effects : queue
Circular queue implementation using array.
; void insert(int queue[], int *rear, int front, int value) { *rear= (*rear +1) % MAX; if(*rear == front) { printf("The queue is full\n"); exit(0); } queue[*rear] = value; } void deleteQ(int queue[], int *front
Queue implementation using linked list.
Description: The advantage of using linked list is that there is no size limit. The size of queue grow and shrink as per insertion and deletion takes... == NULL)) { printf(" The queue is empty can not delete Error\n"
java persistence example
java persistence example  java persistence example
Email queue while sending mail using Struts Class
Email queue while sending mail using Struts Class  Can I maintain a queue of mails being sent from a JSP page in a DB to get its status
Email queue while sending mail using Struts Class
Email queue while sending mail using Struts Class  Can I maintain a queue of mails being sent from a JSP page in a DB to get its status
Java Client Application example
Java Client Application example  Java Client Application example
How to match a string using the INDEX and the method use Stack or Queue?
How to match a string using the INDEX and the method use Stack or Queue? ... of character that match the given data by INDEX position.using the STACK or QUEUE THE MATCH OF THE STRING IS BASED ON THERE INDEX POSITION **Example output
Example of HashSet class in java
Example of HashSet class in java. In this part of tutorial, we... unique. You can not store duplicate value. Java hashset example. How.... Example of Hashset iterator method in java. Example of Hashset size() method
Example of HashMap class in java
Example of HashMap class in java. The HashMap is a class in java collection framwork. It stores values in the form of key/value pair. It is not synchronized
Java FTP Client Example
Java FTP Client Example  How to write Java FTP Client Example code? Thanks   Hi, Here is the example code of simple FTP client in Java which downloads image from server FTP Download file example. Thanks
Advertisements
 

 

 

DMCA.com