Java 中队列的使用示例及常用方法示例和比较

在java5中新增加了java.util.Queue接口,用以支持队列的常见操作。

Queue接口与List、Set同一级别,都是继承了Collection接口。
Queue使用时要尽量避免Collection的add()和remove()方法,而是要使用offer()来加入元素,使用poll()来获取并移出元素。
新加的方法肯定有好处,不然干嘛加他,直接用add和remove方法不就好啦。add()和remove()方法在失败的时候会抛出异常。
 如果要使用前端而不移出该元素,使用 element()或者peek()方法。
值得注意的是LinkedList类实现了Queue接口,因此我们可以把LinkedList当成Queue来用。
LinkedList实现了Queue接口。

附上使用的示例代码,如下:

import java.util.LinkedList;
import java.util.Queue;

class Hello {
    public static void main(String[] a) {
        Queue<String> queue = new LinkedList<>();
        queue.offer("1");//插入一个元素
        queue.offer("2");
        queue.offer("3");
        //打印元素个数
        System.out.println("queue.size()  " + queue.size());//queue.size()  3
        //遍历打印所有的元素,按照插入的顺序打印
        for (String string : queue) {
            System.out.println(string);
        }
        System.out.println("queue.size()  " + queue.size());//queue.size()  3   上面只是简单循环,没改变队列

        String getOneFrom1 = queue.element();
        System.out.println("getOneFrom1  " + getOneFrom1);//getOneFrom1  1          因为使用前端而不移出该元素
        System.out.println("queue.size()  " + queue.size());//queue.size()  3       队列变啦才怪

        String getOneFrom2 = queue.peek();
        System.out.println("getOneFrom2  " + getOneFrom2);//getOneFrom2  1          因为使用前端而不移出该元素
        System.out.println("queue.size()  " + queue.size());//queue.size()  3       队列变啦才怪

        String getOneFrom3 = queue.poll();
        System.out.println("getOneFrom3  " + getOneFrom3);//getOneFrom3  1          获取并移出元素
        System.out.println("queue.size()  " + queue.size());//queue.size()  2       队列变啦
    }
}
然后就是证明上面的结论是正确的,看看了队列这个接口的代码,如下:真不多。一共就六个方法。

//摘自Java 1.8 源码,注释摘一半不到.这个接口,就这么几个方法,好幸福啊,这么少。
public interface Queue<E> extends Collection<E> {

    /**
     * Inserts the specified element into this queue if it is possible to do so
     * immediately without violating capacity restrictions, returning
     * {@code true} upon success and throwing an {@code IllegalStateException}
     * if no space is currently available.
     * ......
     */
    boolean add(E e);//比offer多丢了个异常,跟容量相关

    /**
     * Inserts the specified element into this queue if it is possible to do
     * so immediately without violating capacity restrictions.
     * When using a capacity-restricted queue, this method is generally
     * preferable to {@link #add}, which can fail to insert an element only
     * by throwing an exception.
     * ......
     */
    boolean offer(E e);

    /**
     * Retrieves and removes the head of this queue.  This method differs
     * from {@link #poll poll} only in that it throws an exception if this
     * queue is empty.
     * ......
     */
    E remove();//与poll的差别是当队列是空的时候,这个要报异常,下面返回null

    /**
     * Retrieves and removes the head of this queue,
     * or returns {@code null} if this queue is empty.
     * ....
     */
    E poll();

    /**
     * Retrieves, but does not remove, the head of this queue.  This method
     * differs from {@link #peek peek} only in that it throws an exception
     * if this queue is empty.
     * ......
     */
    E element();//当队列空的时候异常,下面的只是返回null

    /**
     * Retrieves, but does not remove, the head of this queue,
     * or returns {@code null} if this queue is empty.
     * ......
     */
    E peek();

}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值