Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Java Java Data Structures - Retired Efficiency! Call Center Queue

Rob Bridges
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Rob Bridges
Full Stack JavaScript Techdegree Graduate 35,467 Points

Why does .poll() not work to determine if the queue is empty in the call center Challenge?

Hey, Craig Dennis , in regards to the call center Challenge, why does

while(mSupportReps.peek() == null) {
          playHoldMusic();
    }
    csr = mSupportReps.poll();
    csr.assist(customer);
    mSupportReps.add(csr);

Work, but.

while(mSupportReps.poll() == null) {
   playHoldMusic();
}
csr = mSupportReps.poll();
csr.assist(customer);
mSupportReps.add(csr);

Does not?

Since they both return null if the queue is empty shouldn't that way work just as well? I used code from the question asked earlier about this, I'm just wondering why we couldn't use the .poll() method that I originally used was different in some way that it wasn't able to be used.

Thanks!

2 Answers

Christopher Augg
Christopher Augg
21,223 Points

Heya Rob,

The reason is because poll() removes the item from the queue. The equality for while(mSupportReps.poll() == null) will iterate until a successful poll occurs but when the csr = mSupportReps.poll(); is called, csr will be assigned null if nothing else is in the queue.

I wrote up an example in Eclipse to show how the item in the queue is lost due to the poll() being called in the while :

       public static void main(String[] args) {

    Queue<String> myQ = new LinkedList<String>();

             // add name to queue
            myQ.add("Chris");

             // name is in the queue. Therefore, the poll() removes it from the queue and breaks out of while
    while(myQ.poll() == null) {
        System.out.println("Null");                                 
    }

            // name is no longer in the queue. Therefore, name is null
    String name = myQ.poll();
    System.out.println("Hello " + name);

}

I hope that helps.

Hello,

There is a way to get poll to work with the while loop, you just need to have the assignment happen in the conditional statement. For example:

public void queueTest(Queue<String> stringQueue) {
     String myString;
     while ((myString = stringQueue.poll()) == null)
           System.out.println("Queue is empty");

     System.out.println("The string is: " + myString);
}

So both peek() and poll() return the first item in the queue. However, as mentioned earlier, poll() actually removes it from the top as well while peek() leaves it as the first item in the queue.