-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQueueBehavior.java
More file actions
50 lines (41 loc) · 1.39 KB
/
Copy pathQueueBehavior.java
File metadata and controls
50 lines (41 loc) · 1.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package com;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.PriorityQueue;
import java.util.Queue;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ConcurrentLinkedDeque;
import java.util.concurrent.LinkedBlockingDeque;
import java.util.concurrent.PriorityBlockingQueue;
public class QueueBehavior {
static public class Gen implements Iterator<String> {
int i = 0;
String[] s = ("one two three four five six seven").split(" ");
@Override
public boolean hasNext() {
return i < s.length;
}
@Override
public String next() {
return s[i++];
}
}
private static int count = 10;
public static <T> void test(Queue<T> queue, Iterator<T> gen) {
while (gen.hasNext()) {
queue.offer(gen.next());
}
while (queue.peek() != null) {
System.out.print(queue.remove() + " ");
}
System.out.println();
}
public static void main(String[] args) {
test(new LinkedList<String>(), new Gen());
test(new PriorityQueue<String>(), new Gen());
test(new ArrayBlockingQueue<String>(count), new Gen());
test(new ConcurrentLinkedDeque<String>(), new Gen());
test(new LinkedBlockingDeque<String>(), new Gen());
test(new PriorityBlockingQueue<String>(), new Gen());
}
}