OnJava8-Examples/collectiontopics/QueueBehavior.java

51 lines
1.7 KiB
Java
Raw Normal View History

2016-12-30 22:22:39 -08:00
// collectiontopics/QueueBehavior.java
// (c)2021 MindView LLC: see Copyright.txt
2015-11-15 15:51:35 -08:00
// 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.
2017-01-09 14:26:12 -08:00
// Compares basic behavior
2015-06-15 17:47:35 -07:00
import java.util.*;
2017-01-09 14:26:12 -08:00
import java.util.stream.*;
import java.util.concurrent.*;
2015-06-15 17:47:35 -07:00
public class QueueBehavior {
2017-01-09 14:26:12 -08:00
static Stream<String> strings() {
return Arrays.stream(
("one two three four five six seven " +
"eight nine ten").split(" "));
}
static void test(int id, Queue<String> queue) {
System.out.print(id + ": ");
strings().forEach(queue::offer);
2015-06-15 17:47:35 -07:00
while(queue.peek() != null)
System.out.print(queue.remove() + " ");
System.out.println();
}
public static void main(String[] args) {
2017-01-09 14:26:12 -08:00
int count = 10;
test(1, new LinkedList<>());
test(2, new PriorityQueue<>());
test(3, new ArrayBlockingQueue<>(count));
test(4, new ConcurrentLinkedQueue<>());
test(5, new LinkedBlockingQueue<>());
test(6, new PriorityBlockingQueue<>());
test(7, new ArrayDeque<>());
test(8, new ConcurrentLinkedDeque<>());
test(9, new LinkedBlockingDeque<>());
test(10, new LinkedTransferQueue<>());
test(11, new SynchronousQueue<>());
2015-06-15 17:47:35 -07:00
}
2015-09-07 11:44:36 -06:00
}
/* Output:
2017-01-09 14:26:12 -08:00
1: one two three four five six seven eight nine ten
2: eight five four nine one seven six ten three two
3: one two three four five six seven eight nine ten
4: one two three four five six seven eight nine ten
5: one two three four five six seven eight nine ten
6: eight five four nine one seven six ten three two
7: one two three four five six seven eight nine ten
8: one two three four five six seven eight nine ten
9: one two three four five six seven eight nine ten
10: one two three four five six seven eight nine ten
11:
2015-09-07 11:44:36 -06:00
*/