Menu
Topics Index
...
`

Creating LinkedBlockingDeque and Iterator for it


If you need explanation Read this topic

If you need Answer Take test on this topic

Creating LinkedBlockingDeque and Iterator for it
What will be the output of the following program?
import java.util.*;
import java.util.concurrent.*;
public class BlockingDequeTest {
    public static void main(String... args) {
        LinkedBlockingDeque lbd = new LinkedBlockingDeque();
        lbd.add("B");
        lbd.add("A");
        lbd.add("C");
        lbd.add("D");
        lbd.addFirst("E");
        lbd.addLast("F");
        List list = new ArrayList();
        lbd.drainTo(list, 2);
        show(list.iterator(), "List");
        show(lbd.iterator(), "LBL");
    }
    public static void show(Iterator itr, String msg) {
        while (itr.hasNext()) {
            String s = (String) itr.next();
            System.out.print(s + " ");
            itr.remove();
        }
        System.out.println();
    }
}

A. E B 
A C D F
B. A C D F 
E B
C. E F
B A C D
D. Some other output
E. Compilation Error or Runtime Error
Topic: Other Advanced Topics In Java

If you need explanation Read this topic

If you need Answer Take test on this topic

User comments below. All of them might not be correct.

Ans is D..intialy BACD are in list we are adding E strting and F ending..bt using drainTO we are using only 2 elements so OP EB EB

Posted by Uday Kumar    2014-07-23 09:11:05


Answer a.
dequeue initial order EBACDF.
show 1st method prints  1st 2 char E B bcz drainto list ve 2 .
2 nd method prints remaining ACDF

Posted by NagaRaj Nataraj    2014-07-23 09:18:51


A
LinkedBlockingDeque is FIFO
adds element from head removes from tail
collection [EBACDF]
drain to will remove 2 elements (starting from head E B) add into list
ur running iterator on list first
and then on lbd (now contains ACDF)
output
E B
A C D F

Posted by Tushar Ghude    2014-07-23 09:45:18


A

Posted by Himani Mittal    2014-07-24 02:56:16


Congratulations NagaRaj Nataraj. You are this dose winner. We will send you the link using which you can claim your recharge.

Posted by Merit Campus    2014-07-24 04:47:41


© meritcampus 2019

All Rights Reserved.

Open In App