java如何实现FIFO先进先出的队列呢?

欣喜 Java经验 发布时间:2025-01-24 10:47:26 阅读数:12429 1
下文笔者讲述java代码实现一个先进先出的队列的方法

先进先出的实现示例

 简单实现队列先进先出
 
    package com;
     
    import java.util.Linkedlist;
     
    public class MyQueue
    {
        private LinkedList list = new LinkedList();
     
        public void put(Object t){ //加入数据
            list.addFirst(t);
        }
        public Object get(){ //取出先加入的数据
            return  list.removeLast();
        }
        public boolean isEmpt(){
           return  list.isEmpty();
        }
     
        public int getSize(){
            return  list.size();
        }
    }

 运行:

     @Test
        public void queue () throws Exception
        {
            MyQueue myQueue = new MyQueue();
           for(int i = 0; i<10;i++){
               myQueue.put(Integer.toString(i));
           }
     
           while(!myQueue.isEmpt()){
               System.out.println("当前取出队列数据: "+myQueue.get());
           }
            System.out.println("当前队列数据长度: "+myQueue.getSize());
            System.out.println("当前队列数据是否为空: "+myQueue.isEmpt());
        }
    package linkedlisttest;
     
    import java.util.ArrayList;
    import java.util.Deque;
    import java.util.LinkedList;
    import java.util.List;
      
    public class FIFOTest {
     
        /**
         * @param args the command line arguments
         */
        public static void main(String[] args) {
            FIFO<A> fifo = new FIFOImpl<A>(5);
            for (int i = 0; i < 20; i++) {
                A a = new A("A:" + i);
                A head = fifo.addLastSafe(a);
                System.out.println(i + "\thead:" + head + "\tsize:" + fifo.size());
            }
     
            System.out.println("---------------");
     
            System.out.println("弹出数据");
            List<A> polls = fifo.setMaxSize(3);
            for (A a : polls) {
                System.out.println("\thead:" + a);
            }
             
            System.out.println("剩余数据");
            for (A a : fifo) {
                System.out.println("\thead:" + a);
            }
            System.out.println(fifo.size());
        }
    }
     
    interface FIFO<T> extends List<T>, Deque<T>, Cloneable, java.io.Serializable {
     
        /**
         * 向最后添加一个新的,如果长度超过允许的最大值,则弹出一个 *
         */
        T addLastSafe(T addLast);
     
        /**
         * 弹出head,如果Size = 0返回null。而不同于pop抛出异常
         * @return 
         */
        T pollSafe();
     
        /**
         * 获得最大保存
         *
         * @return
         */
        int getMaxSize();
     
        /**
         * 设置最大存储范围
         *
         * @return 返回的是,因为改变了队列大小,导致弹出的head
         */
        List<T> setMaxSize(int maxSize);
     
    }
     
    class FIFOImpl<T> extends LinkedList<T> implements FIFO<T> {
     
        private int maxSize = Integer.MAX_VALUE;
        private final Object synObj = new Object();
     
        public FIFOImpl() {
            super();
        }
     
        public FIFOImpl(int maxSize) {
            super();
            this.maxSize = maxSize;
        }
     
        @Override
        public T addLastSafe(T addLast) {
            synchronized (synObj) {
                T head = null;
                while (size() >= maxSize) {
                    head = poll();
                }
                addLast(addLast);
                return head;
            }
        }
     
        @Override
        public T pollSafe() {
            synchronized (synObj) {
                return poll();
            }
        }
     
        @Override
        public List<T> setMaxSize(int maxSize) {
            List<T> list = null;
            if (maxSize < this.maxSize) {
                list = new ArrayList<T>();
                synchronized (synObj) {
                    while (size() > maxSize) {
                        list.add(poll());
                    }
                }
            }
            this.maxSize = maxSize;
            return list;
        }
     
        @Override
        public int getMaxSize() {
            return this.maxSize;
        }
    }
     
    class A {
     
        private String name;
     
        public A() {
        }
     
        public A(String name) {
            this.name = name;
        }
     
        public String getName() {
            return name;
        }
     
        public void setName(String name) {
            this.name = name;
        }
     
        @Override
        public String toString() {
            return "A{" + "name=" + name + '}';
        }
    }
版权声明

本文仅代表作者观点,不代表本站立场。
本文系作者授权发表,未经许可,不得转载。

本文链接: https://www.Java265.com/JavaJingYan/202501/17376871828222.html

最近发表

热门文章

好文推荐

Java265.com

https://www.java265.com

站长统计|粤ICP备14097017号-3

Powered By Java265.com信息维护小组

使用手机扫描二维码

关注我们看更多资讯

java爱好者