Search code examples
javaqueuebackpeek

Queue unexpectedly puts last element in the front when I call poll()


I have the following code. I'm trying to do a breadth first search of a tree that I created call HabitItem.

Here is queue traveral

import java.io.File;
import java.util.*;
import java.util.concurrent.PriorityBlockingQueue;
public static void traverseQueue() {
    queueHabitItems.add(head);
    while(!queueHabitItems.isEmpty()){
        HabitItem node = queueHabitItems.peek();
        for(int i = 0; i < node.children.size(); i++)
        {
            HabitItem it = node.children.get(i);
            System.out.print("node: ");
            System.out.print(node.name);
            System.out.print(", child: ");
            System.out.println(it.name);
            queueHabitItems.offer(it);
        }
        queueHabitItems.poll();
        System.out.println("Something Good Is Coming");
    }
}

I'm trying to implement a Queue. And I'm using the Poll() function to remove elements. Suppose I have the following Data in the Queue.

A B C D E

And I want to use poll() to remove the front element. Well for some reason, E goes to the front of the list, such that it becomes

E B C D

Am I doing something wrong or is there something that I don't fundamentally understand about Queues? Shouldn't B be the front item?

public class myMain {

static PriorityQueue<HabitItem> queueHabitItems = new    PriorityQueue<HabitItem>();

static HabitItem head = new HabitItem("A");
static HabitItem B = new HabitItem("B");
static HabitItem C = new HabitItem("C");
static HabitItem D = new HabitItem("D");
static HabitItem E = new HabitItem("E");
static HabitItem F = new HabitItem("F");
static HabitItem G = new HabitItem("G");
static HabitItem H = new HabitItem("H");
static HabitItem I = new HabitItem("I");
static HabitItem J = new HabitItem("J");
static HabitItem K = new HabitItem("K");
static HabitItem L = new HabitItem("L");
static HabitItem M = new HabitItem("M");
static HabitItem N = new HabitItem("N");
static HabitItem O = new HabitItem("O");

public static void hardCodeHabits() {
    System.out.print(D.id);


    head.children.add(B);
    head.children.add(C);
    head.children.add(D);
    head.children.add(E);
    B.children.add(F);
    B.children.add(G);
    B.children.add(H);
    C.children.add(I);
    B.children.add(J);
    E.children.add(K);
    I.children.add(L);
    L.children.add(N);
    L.children.add(M);
    N.children.add(O);
    System.out.print(D.id);

}

public static void traverseQueue() {
    queueHabitItems.add(head);
    while(!queueHabitItems.isEmpty()){
        HabitItem node = queueHabitItems.peek();
        for(int i = 0; i < node.children.size(); i++)
        {
            HabitItem it = node.children.get(i);
            System.out.print("node: ");
            System.out.print(node.name);
            System.out.print(", child: ");
            System.out.println(it.name);
            queueHabitItems.offer(it);
        }
        queueHabitItems.remove();
        System.out.println("Something Good Is Coming");
    }
}

public static void main(String[] args) {
    System.out.println("Hello world");
    hardCodeHabits();
    traverseQueue();
    try{
        Scanner x = new Scanner(new File("justText.txt"));
        Formatter y = new Formatter(new File("output.txt"));

        y.format("%s", "hey");
        y.close();

        while (x.hasNext()) {
            System.out.println(x.next());
        }
    }
    catch(Exception e) {
        System.out.println("Couldn't open the file!");
    }

}


}

This is my HabitItems class

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
public class HabitItem implements Comparable<HabitItem> {
static int Counter = 0;
String name;
boolean completed[] = new boolean[7];
List<HabitItem> children = new ArrayList<HabitItem>();
int id;


public String getName(){
    return name;
}
public boolean[] getCompleted(){
    return completed;
}
public void setCompleted(int index, boolean checked){
    completed[index] = checked;
}

public boolean isChecked(int index){
    return completed[index];
}

public HabitItem(String name) {
    super();
    Counter++;
    this.id = Counter;
    this.name = name;
    for(int i = 0; i < 7; i++){
        completed[i] = false;
    }
}

public HabitItem(int id) {
    super();
    Counter++;
    this.id = id;
}

public int compareTo(HabitItem o) {
    if(o.id == this.id)
    {
        return 1;
    }
    else
    {
        return 0;
    }
}

}

Solution

  • Again, don't use peek, use remove:

        while (!queueHabitItems.isEmpty()) {
            //!! HabitItem node = queueHabitItems.peek();
            HabitItem node = queueHabitItems.remove();
            for (int i = 0; i < node.children.size(); i++) {
                HabitItem it = node.children.get(i);
                System.out.print("node: ");
                System.out.print(node.name);
                System.out.print(", child: ");
                System.out.println(it.name);
                queueHabitItems.offer(it);
            }
            // !! queueHabitItems.remove();
            System.out.println("Something Good Is Coming");
        }
    

    The peek call just peeks into the collection but does not respect the priority. The poll and remove do respect this.