• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

Regarding java.util.Queue

 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Chandra Bhatt
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


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
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Jim,

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


Thanks and Regards,
Kris.
 
I think he's gonna try to grab my monkey. Do we have a monkey outfit for this tiny ad?
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic