java 队列、优先级队列、双向队列示例演示代码
复制代码
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113package org.rui.collection2.queues; import java.util.LinkedList; import java.util.PriorityQueue; import java.util.Queue; import java.util.concurrent.ArrayBlockingQueue; import java.util.concurrent.ConcurrentLinkedQueue; import java.util.concurrent.LinkedBlockingQueue; import java.util.concurrent.PriorityBlockingQueue; import org.rui.generics.anonymity.Generator; /** * 下面涉及Queue实现的大部分操作的基本示例 * 可以看到除了优先队列,Queue将精确地按照元素被置于Queue中的顺序产生它们 * @author lenovo * */ public class QueueBeHavior { private static int count=10; static<T> void test(Queue<T> queue,Generator<T> gen) { for(int i=0;i<count;i++) { //T temp=gen.next(); //System.out.println(temp); queue.offer(gen.next()); } while(queue.peek()!=null) System.out.print(queue.remove()+" "); System.out.println(); } static class Gen implements Generator<String> { String[] s= ("one tow three four five six seven eight nine ten".split(" ")); int i; public String next() { return s[i++]; } } 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 ConcurrentLinkedQueue<String>(),new Gen()); test(new LinkedBlockingQueue<String>(),new Gen()); test(new PriorityBlockingQueue<String>(),new Gen()); } } /**output: one tow three four five six seven eight nine ten eight five four nine one seven six ten three tow one tow three four five six seven eight nine ten one tow three four five six seven eight nine ten one tow three four five six seven eight nine ten eight five four nine one seven six ten three tow */
复制代码
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66package org.rui.collection2.queues; import java.util.PriorityQueue; /** * 优先级队列 * 主要和次要的优先级排序 * 该列表的排序顺序也是通过实现Comparable而进行控制的 * @author lenovo * */ public class ToDoList extends PriorityQueue<ToDoList.ToDoItem> { static class ToDoItem implements Comparable<ToDoItem> { private char primary;//主要的 private int secondary;//二 private String item; public ToDoItem(String item,char primary, int secondary) { this.primary = primary; this.secondary = secondary; this.item = item; } public int compareTo(ToDoItem o) { if(primary>o.primary)//先比较主要的 return +1; if(primary==o.primary) if(secondary>o.secondary) //再比较次要的 return +1; else if(secondary==o.secondary) return 0; return -1; } public String toString() { return Character.toString(primary)+secondary+": "+item; } } public void add(String td,char pri,int sec) { super.add(new ToDoItem(td, pri, sec)); } public static void main(String[] args) { ToDoList to=new ToDoList(); to.add("Empty Trash",'C',4); to.add("Feed dog",'A',2); to.add("Feed bird",'B',7); to.add("Mow lawn",'C',3); to.add("Water lawn",'A',1); to.add("Feed cat",'B',1); while(!to.isEmpty()) { System.out.println(to.remove()); } } } /**output: A1: Water lawn A2: Feed dog B1: Feed cat B7: Feed bird C3: Mow lawn C4: Empty Trash */
复制代码
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70package org.rui.collection2.queues; import java.util.LinkedList; /** * 双向队列就是一个队列,但是你可以在任何一端添加或移除元素, * LinkedList无法实现这样的接口,但可以使用组合来创建一个Deque类, * @author lenovo * * @param <T> */ public class Deque<T> { private LinkedList<T> deque=new LinkedList<T>(); public void addFirst(T e){deque.addFirst(e);} public void addLast(T e){deque.addLast(e);} public T getFirst(T e){return deque.getFirst();} public T getLast(T e){return deque.getLast();} public T removeFirst(){return deque.removeFirst();} public T removeLast(){return deque.removeLast();} public int size(){return deque.size();} public String toString(){return deque.toString();} //and other methods as necessary............ public static void fillTest(Deque<Integer> de) { for(int i=10;i<17;i++) de.addFirst(i); for(int i=50;i<55;i++) de.addLast(i); } public static void main(String[] args) { Deque<Integer> deque=new Deque<Integer>(); fillTest(deque); System.out.println(deque); while(deque.size()!=0) System.out.print(deque.removeFirst()+" "); System.out.println(); fillTest(deque); while(deque.size()!=0) System.out.print(deque.removeLast()+" "); System.out.println(); } } /** output: [16, 15, 14, 13, 12, 11, 10, 50, 51, 52, 53, 54] 16 15 14 13 12 11 10 50 51 52 53 54 54 53 52 51 50 10 11 12 13 14 15 16 */
最后
以上就是眯眯眼溪流最近收集整理的关于java 队列、优先级队列、双向队列示例演示代码的全部内容,更多相关java内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复