Java Queue Example

Ads

 
 

Share on Google+Share on Google+

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

how to use queue in java ?

Ads
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.


Ads









Related Tutorials/Questions & Answers:
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
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
Advertisements
queue in java
queue in java  how to performe all queue operation?   Please visit the following link: Queue Example in Java
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
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
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
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
java persistence example
java persistence example  java persistence example
Java Client Application example
Java Client Application example  Java Client Application example
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
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
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
What is array in java with example?
What is array in java with example?  Hi, I am beginner in Java and want to learn Java Array concepts. Which is the best tutorials for learn Java Array with example codes? What is array in java with example? Thanks
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
FTP Java example
FTP Java example  Where is the FTP Java example on your website? I am... examples of FTP at: FTP Programming in Java tutorials with example code. Thaks... functionality in my Java based application. My application is swing based and I have
java program example - Java Beginners
java program example  can we create java program without static and main?can u plzz explain with an example
Java FTP Example
Java FTP Example  Is there any java ftp example and tutorials... and tutorials that teaches you how to user FTP in your Java project. Most commonly used FTP api in java is Apache FTP. Browse all the FTP tutorials at Java FTP
freemarker example - Java Beginners
an example for freemarker. i want to get the values from java and display those values in the page designed using freemarker(how to get the values from java). and please provide an example with code and directory structure. send me ASAP
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
Example Code - Java Beginners
Example Code  I want simple Scanner Class Example in Java and WrapperClass Example. What is the Purpose of Wrapper Class and Scanner Class . when i compile the Scanner Class Example the error occur : Can not Resolve symbol

Ads

 
Advertisement null

Ads