66 lines
2.0 KiB
Java
Raw Normal View History

2016-08-02 11:52:46 -06:00
// understandingcollections/SimpleDeques.java
2016-07-05 14:46:09 -06:00
// (c)2016 MindView LLC: see Copyright.txt
// We make no guarantees that this code is fit for any purpose.
2016-09-23 13:23:35 -06:00
// Visit http://OnJava8.com for more book information.
2016-07-05 14:46:09 -06:00
// Very basic test of Deques
import java.util.concurrent.*;
import java.util.*;
import java.util.function.*;
class CountString implements Supplier<String> {
private int n = 0;
public CountString() {}
public CountString(int start) { n = start; }
@Override
public String get() { return Integer.toString(n++); }
}
public class SimpleDeques {
static void test(Deque<String> 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
*/