// collectiontopics/SimpleDeques.java // (c)2021 MindView LLC: see Copyright.txt // We make no guarantees that this code is fit for any purpose. // Visit http://OnJava8.com for more book information. // Very basic test of Deques import java.util.*; import java.util.concurrent.*; import java.util.function.*; class CountString implements Supplier { private int n = 0; CountString() {} CountString(int start) { n = start; } @Override public String get() { return Integer.toString(n++); } } public class SimpleDeques { static void test(Deque deque) { CountString s1 = new CountString(), s2 = new CountString(20); for(int n = 0; n < 8; n++) { deque.offerFirst(s1.get()); deque.offerLast(s2.get()); // Same as offer() } System.out.println(deque); String result = ""; while(deque.size() > 0) { System.out.print(deque.peekFirst() + " "); result += deque.pollFirst() + " "; System.out.print(deque.peekLast() + " "); result += deque.pollLast() + " "; } System.out.println("\n" + result); } public static void main(String[] args) { int count = 10; System.out.println("LinkedList"); test(new LinkedList<>()); System.out.println("ArrayDeque"); test(new ArrayDeque<>()); System.out.println("LinkedBlockingDeque"); test(new LinkedBlockingDeque<>(count)); System.out.println("ConcurrentLinkedDeque"); test(new ConcurrentLinkedDeque<>()); } } /* Output: LinkedList [7, 6, 5, 4, 3, 2, 1, 0, 20, 21, 22, 23, 24, 25, 26, 27] 7 27 6 26 5 25 4 24 3 23 2 22 1 21 0 20 7 27 6 26 5 25 4 24 3 23 2 22 1 21 0 20 ArrayDeque [7, 6, 5, 4, 3, 2, 1, 0, 20, 21, 22, 23, 24, 25, 26, 27] 7 27 6 26 5 25 4 24 3 23 2 22 1 21 0 20 7 27 6 26 5 25 4 24 3 23 2 22 1 21 0 20 LinkedBlockingDeque [4, 3, 2, 1, 0, 20, 21, 22, 23, 24] 4 24 3 23 2 22 1 21 0 20 4 24 3 23 2 22 1 21 0 20 ConcurrentLinkedDeque [7, 6, 5, 4, 3, 2, 1, 0, 20, 21, 22, 23, 24, 25, 26, 27] 7 27 6 26 5 25 4 24 3 23 2 22 1 21 0 20 7 27 6 26 5 25 4 24 3 23 2 22 1 21 0 20 */