GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing www.tutorialspoint.com — Java PriorityQueue iterator() Method
Selected Reading
Java PriorityQueue iterator() MethodDescriptionThe Java PriorityQueue iterator() method returns an iterator over the elements in this queue. This iterator then can be used to iterate each element of the PriorityQueue object. DeclarationFollowing is the declaration for java.util.PriorityQueue.iterator() method public Iterator<E> iterator() ParametersNA Return ValueThis method returns an iterator over the elements in this queue. ExceptionNA Iterating PriorityQueue of Ints using iterator() Method ExampleThe following example shows the usage of Java PriorityQueue iterator() method to iterate Integers. We're adding couple of Integers to the PriorityQueue object using add() method calls per element and then iterate and print each element to show the elements added. package com.tutorialspoint;
import java.util.PriorityQueue;
import java.util.Iterator;
public class PriorityQueueDemo {
public static void main(String[] args) {
// create an empty priority queue
PriorityQueue<Integer> queue = new PriorityQueue<>();
// use add() method to add elements in the queue
queue.add(15);
queue.add(22);
queue.add(25);
queue.add(20);
// iterator() is used to print all the elements
// next() returns the next element on each iteration
System.out.println("printing elements using iterator:");
for(Iterator<Integer> itr = queue.iterator();itr.hasNext();) {
System.out.println(itr.next());
}
}
}
OutputLet us compile and run the above program, this will produce the following result − printing elements using iterator: 15 20 25 22 Iterating PriorityQueue of Strings using iterator() Method ExampleThe following example shows the usage of Java PriorityQueue iterator() method to iterate Strings. We're adding couple of Strings to the PriorityQueue object using add() method calls per element and then iterate and print each element to show the elements added. package com.tutorialspoint;
import java.util.PriorityQueue;
import java.util.Iterator;
public class PriorityQueueDemo {
public static void main(String[] args) {
// create an empty priority queue
PriorityQueue<String> queue = new PriorityQueue<>();
// use add() method to add elements in the queue
queue.add("A");
queue.add("B");
queue.add("C");
queue.add("D");
// iterator() is used to print all the elements
// next() returns the next element on each iteration
System.out.println("printing elements using iterator:");
for(Iterator<String> itr = queue.iterator();itr.hasNext();) {
System.out.println(itr.next());
}
}
}
OutputLet us compile and run the above program, this will produce the following result − printing elements using iterator: A B C D Iterating PriorityQueue of Objects using iterator() method ExampleThe following example shows the usage of Java PriorityQueue iterator() method to iterate Student objects. We're adding couple of Student objects to the PriorityQueue object using add() method calls per element and then iterate and print each element to show the elements added. package com.tutorialspoint;
import java.util.PriorityQueue;
import java.util.Iterator;
public class PriorityQueueDemo {
public static void main(String[] args) {
// create an empty priority queue
PriorityQueue<Student> queue = new PriorityQueue<>();
// use add() method to add elements in the queue
queue.add(new Student(1, "Julie"));
queue.add(new Student(2, "Robert"));
queue.add(new Student(3, "Adam"));
// iterator() is used to print all the elements
// next() returns the next element on each iteration
System.out.println("printing elements using iterator:");
for(Iterator<Student> itr = queue.iterator();itr.hasNext();) {
System.out.println(itr.next());
}
}
}
class Student implements Comparable<Student> {
int rollNo;
String name;
Student(int rollNo, String name){
this.rollNo = rollNo;
this.name = name;
}
@Override
public String toString() {
return "[ " + this.rollNo + ", " + this.name + " ]";
}
@Override
public boolean equals(Object obj) {
Student s = (Student)obj;
return this.rollNo == s.rollNo && this.name.equalsIgnoreCase(s.name);
}
@Override
public int compareTo(Student student) {
return this.rollNo - student.rollNo;
}
}
OutputLet us compile and run the above program, this will produce the following result − printing elements using iterator: [ 1, Julie ] [ 2, Robert ] [ 3, Adam ] java_util_priorityqueue.htm
Advertisements
|
| You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |