How to iterate by priority?

In java let say I have a class called ExampleClass, which has a collection of a class called Item. Each item has an integer in a field called ID. How can I have the iterator of ExampleClass iterate through Items with even IDs and then iterate through Items with Odd IDs.

Edit: I don't need an answer to this specific problem, I am asking generally how to have an Iterator inner class in ExampleClass that will iterate through items with one property first and then through the others.

You could use the following if condition to check if the ID is odd or not:

for(int i = 0;i < maxID;i++) { //I'm not sure what your maximum ID is.
  if(Item.ID % 2 == 0) {
    //Iterate
  }
}

Then, you can do the same thing for odd IDs:

for(int i = 0;i < maxID;i++) { //I'm not sure what your maximum ID is.
  if(Item.ID % 2 == 1) {
    //Iterate
  }
}

The code Item.ID % 2 divides the ID by 2, and returns the remainder. In the if statement, it checks to see if the remainder is 0. If it is, then the ID is even, and it runs the desired code. However, if the remainder is 1, then the number is odd.

Example: 4 / 2 = 2. The remainder is 0. Therefore, the number 4 is even.

Example: 5 / 2 = 2. The remainder is 1. Therefore, the number 5 is odd.

How to iterate over a PriorityQueue?, From the Javadocs. The Iterator provided in method iterator() is not guaranteed to traverse the elements of the PriorityQueue in any particular� C++ priority_queue does not offer a .begin() pointer (like vector would do) that you can use to iterate over it. If you want to iterate over the priority queue to search for whether it contains a value then maybe create a wrapper priority queue and use a hash set to keep track of what you have in the queue.

Here are two ways you can go about it with 1 liners.

final Iterator<Item> it = items.stream().filter(i.getID() % 2 == 0).iterator();

or

for (final Item i : items.stream().filter(i.getID() % 2 == 0).toArray())
     doSomethingWith(i);

Java, See the below example, we can iterate a PriorityQueue by using. For-each loop; Iterator; Stream (Introduced in Java8). IterateQueueExample. C++ priority_queue does not offer a .begin() pointer (like vector would do) that you can use to iterate over it. If you want to iterate over the priority queue to search for whether it contains a value then maybe create a wrapper priority queue and use a hash set to keep track of what you have in the queue.

If you need to iterate through them in any specific order, you will need to make the array of items ordered before you loop through them. How you go about that is up to you based on your application and ordering needs. I suggest something which resembles the pseudocode:

unsortedArray[]
sortedArray[]

for (every element in unsortedArray) {
    id = element.ID; //Or sort value calculated with some other method
    sortedIndexBeforeID = sortedArray.findLastElementWithIDBelow(id); //Get the index of sorted element with an ID just before ours.
    sortedArray.insert(element,sortedIDBeforeID); //Add the element into the sorted array at given index, pushing the rest of the elements around
}

sortedArray[]

java.util.PriorityQueue.iterator() Method, PriorityQueue.iterator() Method - The iterator() method is used to return an public static void main(String args[]) { // create priority queue PriorityQueue < Integer >� The Java.util.PriorityQueue.iterator() method is used to return an iterator of the same elements as the Priority Queue. The elements are returned in random order from what present in the Queue. Syntax: Iterator iterate_value = Priority_Queue.iterator(); Parameters: The function does not take any parameter.

Java Collection, PriorityQueue Exercises: Iterate through all , Write a Java program to iterate through all elements in priority queue. Sample Solution:- Java Code: import java.util.PriorityQueue; public class� Java Collection, PriorityQueue Exercises: Exercise-2 with Solution. Write a Java program to iterate through all elements in priority queue. Sample Solution:-

Java PriorityQueue, Iterating Over a PriorityQueue. To iterate over the elements of a priority queue, we can use the iterator() method. In� Iterating Over a PriorityQueue To iterate over the elements of a priority queue, we can use the iterator() method. In order to use this method, we must import the java.util.Iterator package.

PriorityQueue (Java Platform SE 7 ), As elements are added to a priority queue, its capacity grows automatically. The details of the growth policy are not specified. This class and its iterator implement � if you also want to iterate, a queue has an incorrect interface. A queue is an adaptor that provides a restricted subset of the original container 2.The definition of a queue is a FIFO and by definition a FIFO is not iterable

Comments
  • items.stream().sorted(Comparator.comparingInt(i -> i.getID() % 2)).iterator()
  • @shmosel too bad it cannot be upvoted or accepted as the answer, since you post answer as comment...