'An implementation of a custom interface identical to the Java method iterator() is not working

I am attempting to implement an interface that is identical to the iterator() method found in Java. For my specific program, the interface is implemented as an inner class PriorityQueueIterator to the outer class PriorityQueueLinked. It has methods hasNext() and next() just like in iterator() and it's design is to iterate through elements of a linked list Priority Queue. What the implementation should be outputting is all the elements within the passed Priority Queue, however, I am only getting the first element of the Priority Queue as an output. Below is the snippet of code:

 public Iterator iterator() {
  return new PriorityQueueIterator(this);
}

class PriorityQueueIterator implements Iterator<Object> {
  
  private Queue<PriorityQueueLinked> q = new LinkedList<PriorityQueueLinked>();
  
  public PriorityQueueIterator(PriorityQueueLinked pq) {
      if(pq.isEmpty()) {
          return;
      }
      else {
          q.add(pq);
      }
  }
  
  public boolean hasNext() {
      return !q.isEmpty();
  }
  
  public Object next() {
      if(hasNext()) {
          PriorityQueueLinked pq = q.remove();
          Object o = new Object();
          if(!pq.isEmpty()) {
              // examine() retrieves the element at the front of the Priority Queue
              o = pq.examine();
          }
          return o;
      }
      else {
          throw new OutOfBounds("No elements in queue.");
      }
  }
}

In the main(String[] args) method:

PriorityQueueIterator it = PriorityQueueLinked.PriorityQueueIterator(PriorityQueue);
while (it.hasNext()) {
     System.out.println(it.next());
}

If I passed a Priority Queue which had an element structure: 6, 9, 1 (note, the commas are only used here to show the separation of elements and in effect the individual links), the output should be 6 9 1. However, I am getting an output of 6. How should I fix this?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source