Java简单实现固定长度队列(FIFO)

[java] view plain copy
  1. /* 
  2.  * To change this template, choose Tools | Templates 
  3.  * and open the template in the editor. 
  4.  */  
  5. package linkedlisttest;  
  6.   
  7. import java.util.ArrayList;  
  8. import java.util.Deque;  
  9. import java.util.LinkedList;  
  10. import java.util.List;  
  11.   
  12. /** 
  13.  * 
  14.  * @author Vicky.H 
  15.  * @email eclipser@163.com 
  16.  */  
  17. public class FIFOTest {  
  18.   
  19.     /** 
  20.      * @param args the command line arguments 
  21.      */  
  22.     public static void main(String[] args) {  
  23.         FIFO<A> fifo = new FIFOImpl<A>(5);  
  24.         for (int i = 0; i < 20; i++) {  
  25.             A a = new A("A:" + i);  
  26.             A head = fifo.addLastSafe(a);  
  27.             System.out.println(i + "\thead:" + head + "\tsize:" + fifo.size());  
  28.         }  
  29.   
  30.         System.out.println("---------------");  
  31.   
  32.         System.out.println("弹出数据");  
  33.         List<A> polls = fifo.setMaxSize(3);  
  34.         for (A a : polls) {  
  35.             System.out.println("\thead:" + a);  
  36.         }  
  37.           
  38.         System.out.println("剩余数据");  
  39.         for (A a : fifo) {  
  40.             System.out.println("\thead:" + a);  
  41.         }  
  42.         System.out.println(fifo.size());  
  43.     }  
  44. }  
  45.   
  46. interface FIFO<T> extends List<T>, Deque<T>, Cloneable, java.io.Serializable {  
  47.   
  48.     /** 
  49.      * 向最后添加一个新的,如果长度超过允许的最大值,则弹出一个 * 
  50.      */  
  51.     T addLastSafe(T addLast);  
  52.   
  53.     /** 
  54.      * 弹出head,如果Size = 0返回null。而不同于pop抛出异常 
  55.      * @return  
  56.      */  
  57.     T pollSafe();  
  58.   
  59.     /** 
  60.      * 获得最大保存 
  61.      * 
  62.      * @return 
  63.      */  
  64.     int getMaxSize();  
  65.   
  66.     /** 
  67.      * 设置最大存储范围 
  68.      * 
  69.      * @return 返回的是,因为改变了队列大小,导致弹出的head 
  70.      */  
  71.     List<T> setMaxSize(int maxSize);  
  72.   
  73. }  
  74.   
  75. class FIFOImpl<T> extends LinkedList<T> implements FIFO<T> {  
  76.   
  77.     private int maxSize = Integer.MAX_VALUE;  
  78.     private final Object synObj = new Object();  
  79.   
  80.     public FIFOImpl() {  
  81.         super();  
  82.     }  
  83.   
  84.     public FIFOImpl(int maxSize) {  
  85.         super();  
  86.         this.maxSize = maxSize;  
  87.     }  
  88.   
  89.     @Override  
  90.     public T addLastSafe(T addLast) {  
  91.         synchronized (synObj) {  
  92.             T head = null;  
  93.             while (size() >= maxSize) {  
  94.                 head = poll();  
  95.             }  
  96.             addLast(addLast);  
  97.             return head;  
  98.         }  
  99.     }  
  100.   
  101.     @Override  
  102.     public T pollSafe() {  
  103.         synchronized (synObj) {  
  104.             return poll();  
  105.         }  
  106.     }  
  107.   
  108.     @Override  
  109.     public List<T> setMaxSize(int maxSize) {  
  110.         List<T> list = null;  
  111.         if (maxSize < this.maxSize) {  
  112.             list = new ArrayList<T>();  
  113.             synchronized (synObj) {  
  114.                 while (size() > maxSize) {  
  115.                     list.add(poll());  
  116.                 }  
  117.             }  
  118.         }  
  119.         this.maxSize = maxSize;  
  120.         return list;  
  121.     }  
  122.   
  123.     @Override  
  124.     public int getMaxSize() {  
  125.         return this.maxSize;  
  126.     }  
  127. }  
  128.   
  129. class A {  
  130.   
  131.     private String name;  
  132.   
  133.     public A() {  
  134.     }  
  135.   
  136.     public A(String name) {  
  137.         this.name = name;  
  138.     }  
  139.   
  140.     public String getName() {  
  141.         return name;  
  142.     }  
  143.   
  144.     public void setName(String name) {  
  145.         this.name = name;  
  146.     }  
  147.   
  148.     @Override  
  149.     public String toString() {  
  150.         return "A{" + "name=" + name + '}';  
  151.     }  
  152. }  

run:
0 head:null size:1
1 head:null size:2
2 head:null size:3
3 head:null size:4
4 head:null size:5
5 head:A{name=A:0} size:5
6 head:A{name=A:1} size:5
7 head:A{name=A:2} size:5
8 head:A{name=A:3} size:5
9 head:A{name=A:4} size:5
10 head:A{name=A:5} size:5
11 head:A{name=A:6} size:5
12 head:A{name=A:7} size:5
13 head:A{name=A:8} size:5
14 head:A{name=A:9} size:5
15 head:A{name=A:10} size:5
16 head:A{name=A:11} size:5
17 head:A{name=A:12} size:5
18 head:A{name=A:13} size:5
19 head:A{name=A:14} size:5
---------------
弹出数据
 head:A{name=A:15}
 head:A{name=A:16}
剩余数据
 head:A{name=A:17}
 head:A{name=A:18}
 head:A{name=A:19}
3
成功构建 (总时间: 0 秒)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值