File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Regarding java.util.Queue Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Regarding java.util.Queue" Watch "Regarding java.util.Queue" New topic
Author

Regarding java.util.Queue

Krishna Potluri
Greenhorn

Joined: Apr 20, 2007
Posts: 19
Hi,

How to read all elements from java.util.Queue using peek()?. Kindly reply me back. Thanks in advance.

One way of getting the elements from java.util.Queue implementation classes is using poll(),but this method removes the elements from the Queue.Code is as follows:
___________________________________________________________________________
//************* with repective to JDK1.5************

import java.util.Queue;
import java.util.LinkedList;
import org.apache.log4j.*;

public class QueueOffer {
private static Category category = Category.getInstance(QueueOffer.class.getName());
public static void main(String[] args) {
Queue queue = new LinkedList();
//Add Items To Queue

queue.offer("Sri");
queue.offer("Krishna");
queue.offer("Prasad");
queue.offer("Potluri");

category.info("...Adding Items To Queue..."+queue.toString());

// Retrive Items From Queue.
category.info("....Retriving The Items From Queue... ");
//Iterator iterate = queue.iterator();
Object o;

while( (o=queue.poll())!= null ) {

category.info(" "+o);

}

}
____________________________________________________________________________

My requirement is just read all elements from the Queue , but elements should not be deleted. This can be done using peek(). But I would like to know how?

Thanks & Regards,
Kris.
Chandra Bhatt
Ranch Hand

Joined: Feb 28, 2007
Posts: 1707
Hi Krishna,


My requirement is just read all elements from the Queue , but elements should not be deleted. This can be done using peek().


peek() returns the top of the elements in the queue.


Simply do this, if you want to read elements from the queue in the order,
peek follows:



peek() doesn't modify the queue; it simply shows an element from the front of the queue.
"Insertion from rear, deletion from front" It's queue


Regards,
cmbhatt
[ April 20, 2007: Message edited by: Chandra Bhatt ]

cmbhatt
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
Remember that a Queue is also a Collection. So you can simply use the iterator() method to iterate through all the values, without removing them. This is also done implicitly by a for loop:

Be aware that the order of iteration is not guaranteed to be the same as the order in which elements would be removed using poll(). For many implementations, including LinkedList, it is the same. But not always.

[Chandra]: "Insertion from rear, deletion from front" It's queue

That's a FIFO queue. The java.util.Queue interface is more general than that; it may instead be a FILO queue (a stack), or a priority queue, or some other ordering.


"I'm not back." - Bill Harding, Twister
Chandra Bhatt
Ranch Hand

Joined: Feb 28, 2007
Posts: 1707

That's a FIFO queue. The java.util.Queue interface is more general than that; it may instead be a FILO queue (a stack), or a priority queue, or some other ordering.



Thanks Jim!

It means queue is not necessarily FIFO. So far Java Queue is concerned, it can be FIFO (general queue), FILO(stack), or priority queue (todo list).


Thanks for clarification, Jim!


Regards,
cmbhatt
Krishna Potluri
Greenhorn

Joined: Apr 20, 2007
Posts: 19
Hi Jim,

Thanks for spending your most valuable time to answer my doubt.


Thanks and Regards,
Kris.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Regarding java.util.Queue