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 spliterator() Method
Selected Reading
Java PriorityQueue spliterator() MethodDescriptionThe Java PriorityQueue spliterator() method creates a late-binding and fail-fast Spliterator for the elements in this queue. Late binding means the spliterator binds to the source of elements at the time of traversal and not when it is created. Spliterator is a better iterator and provides more controls over items during traversal. DeclarationFollowing is the declaration for java.util.PriorityQueue.spliterator() method public Spliterator<E> spliterator() ParametersNA Return ValueThis method returns a Spliterator over the elements in this queue. ExceptionNA Getting Spliterator() to iterate PriorityQueue of Ints ExampleThe following example shows the usage of Java PriorityQueue spliterator() method with Integers. We're creating an PriorityQueue of Integers, adding some elements, and then Spliterator is created using spliterator() method. The iterator is then used to traverse the items and to print them. package com.tutorialspoint;
import java.util.PriorityQueue;
import java.util.Spliterator;
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(25);
queue.add(30);
queue.add(20);
queue.add(18);
// get the spliterator
Spliterator<Integer> spliterator = queue.spliterator();
// use the spliterator to print each item
spliterator.forEachRemaining(i -> System.out.println(i));
}
}
OutputLet us compile and run the above program, this will produce the following result − 18 20 25 30 Getting Spliterator() to iterate PriorityQueue of Strings ExampleThe following example shows the usage of Java PriorityQueue spliterator() method with Strings. We're creating an PriorityQueue of Strings, adding some elements, and then Spliterator is created using spliterator() method. The iterator is then used to traverse the items and to print them. package com.tutorialspoint;
import java.util.PriorityQueue;
import java.util.Spliterator;
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");
// get the spliterator
Spliterator<String> spliterator = queue.spliterator();
// use the spliterator to print each item
spliterator.forEachRemaining(i -> System.out.println(i));
}
}
OutputLet us compile and run the above program, this will produce the following result − A B C D Getting Spliterator() to iterate PriorityQueue of Objects ExampleThe following example shows the usage of Java PriorityQueue spliterator() method with Student objects. We're creating an PriorityQueue of Student objects, adding some elements, and then Spliterator is created using spliterator() method. The iterator is then used to traverse the items and to print them. package com.tutorialspoint;
import java.util.PriorityQueue;
import java.util.Spliterator;
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"));
// get the spliterator
Spliterator<Student> spliterator = queue.spliterator();
// use the spliterator to print each item
spliterator.forEachRemaining(i -> System.out.println(i));
}
}
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 − [ 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. |