Java队列详解之 LinkedList 类

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.2 peek

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

2.3 poll

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

2.4 contains

Returns true if this collection contains the specified element. More formally, returns true if and only if this collection contains at least one element e such that (onull ? enull : o.equals(e)).

3. 简单示例
  • 示例 1
 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;
    }

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

  • 示例2
    public static void main(String[] args) {
        Queue<String> queue = new LinkedList<String>();
        queue.add("118.187.58.34 53281");
        queue.add("219.234.5.128 3128");
        queue.add("113.121.243.38 808");
        System.out.println(queue.size());

        //contains: Returns true if this collection contains the specified element.
        if(queue.contains("113.121.243.38 808")){
            System.out.println("113.121.243.38 808 has been in queue! Don't add again");
        }
    }

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

说文科技

看书人不妨赏个酒钱?

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值