1 package com.bjsxt.others.commons;
2
3 import java.util.Queue;
4
5 import org.apache.commons.collections4.Predicate;
6 import org.apache.commons.collections4.functors.NotNullPredicate;
7 import org.apache.commons.collections4.queue.CircularFifoQueue;
8 import org.apache.commons.collections4.queue.PredicatedQueue;
9 import org.apache.commons.collections4.queue.UnmodifiableQueue;
10
11
12 /**
13 * Queue队列
14 * 1、循环队列
15 * 2、只读队列:不可改变队列
16 * 3、断言队列
17 */
18 public class Demo05 {
19 public static void main(String[] args) {
20 circular();
21 // readOnly(); //会报错,因为只读,不能添加
22 // predicate(); //会报错,不能添加null
23 }
24 /**
25 * 断言队列
26 */
27 public static void predicate() {
28 //循环队列
29 CircularFifoQueue<String> que = new CircularFifoQueue<String>(2);
30 que.add("a");
31 que.add("b");
32 que.add("c");
33 Predicate notNull = NotNullPredicate.INSTANCE;
34 //包装成对应的队列
35 Queue<String> que2 = PredicatedQueue.predicatedQueue(que,notNull);
36 que2.add(null);
37 }
38
39 /**
40 * 只读队列
41 */
42 public static void readOnly() {
43 //循环队列
44 CircularFifoQueue<String> que = new CircularFifoQueue<String>(2);
45 que.add("a");
46 que.add("b");
47 que.add("c");
48 Queue<String> readOnlyQue = UnmodifiableQueue.unmodifiableQueue(que);
49 readOnlyQue.add("d");
50 }
51
52
53 /**
54 * 循环队列
55 */
56 public static void circular() {
57 //循环队列
58 CircularFifoQueue<String> que = new CircularFifoQueue<String>(2);
59 que.add("a");
60 que.add("b");
61 que.add("c");
62 //查看
63 for(int i=0;i<que.size();i++) {
64 System.out.println(que.get(i)); //结果:b c
65 }
66
67 }
68
69 }