Java队列详解之 LinkedList 类

版权声明:如若转载,请联系作者。 https://blog.csdn.net/liu16659/article/details/84501147

Java队列详解之 LinkedList

1. 类简介

  • 类释义

A collection designed for holding elements prior to processing. Besides basic Collection operations, queues provide additional insertion, extraction, and inspection operations. Each of these methods exists in two forms: one throws an exception if the operation fails, the other returns a special value (either null or false, depending on the operation). The latter form of the insert operation is designed specifically for use with capacity-restricted Queue implementations; in most implementations, insert operations cannot fail.

用于在处理前保存元素而设计的 Collection【说明这个是继承自 Collection 接口】

  • 继承关系
    在这里插入图片描述

2. 类方法

2.1 add 方法

Inserts the specified element into this queue if it is possible to do so immediately without violating capacity restrictions, returning true upon success and throwing an IllegalStateException if no space is currently available.

2.1 peek

Retrieves, but does not remove, the head of this queue, or returns null if this queue is empty.

2.1 poll

Retrieves and removes the head of this queue, or returns null if this queue is empty.

3. 简单示例

 public Queue<String> getFreeIpInQueue(WebSite webSite) {
        Queue<String> queue = new LinkedList <String>();
        queue.add("a");
        queue.add("b");
        System.out.println(queue.size());
        for (String s : queue) {
            System.out.println("The content is: "+s);
        }

        //peek: Retrieves, but does not remove, the head of this queue or returns null if this queue is empty.
        System.out.println("peek of queue is: "+queue.peek());
        System.out.println("after peek() "+queue.size());

        //Retrieves and removes the head of this queue, or returns null if this queue is empty.
        System.out.println(""+queue.poll());
        System.out.println("after poll() "+queue.size());
        return queue;
    }

执行结果如下:
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/liu16659/article/details/84501147