Queue特性和基本方法

Queue实现一个先进先出(FIFO, First In First Out)的队列。如收银台排队支付。

Java中LinkedList实现了Queue接口,可以直接把LinkedList当作Queue来使用。

  • 获取队列长度size()
  • 添加元素到队尾boolean add(E e)/boolean offer(E e)
  • 获取队列头部元素并删除 E remove()/E poll()
  • 获取队列头部元素但不删除E element()/E peek()

为什么添加和获取元素提供2组方法?

当添加或获取元素失败时,1个抛出异常,另1个返回false或null。

throw Exception 返回false或null
添加元素到队尾 add(E e) boolean offer(E e)
取对首元素并删除 E remove() E poll()
取队首元素但不删除 E element() E peek()

失败场景示例:

  • 1.如果队列有一个最大长度限制,可能添加失败
//演示代码
Queue q = ...;
if (q.offer("abc")){
//添加成功
}else{
//添加失败
}
  • 2.如果队列是空队列,队首元素获取失败。
//演示代码
Queue q = ...;
if (q.isEmpty()){
//不能获取队首元素
}else{
//可以获取队首元素
}

示例

Person.java

public class Person {
private String name;
private int age;
public Person(String name,int age){
this.name = name;
this.age = age;
}
public String getName(){
return this.name;
}
public int getAge(){
return this.age;
}
@Override
public String toString(){
return "(Person:"+name+","+age+")";
}
public boolean equals(Object o){
if (this == o){
return true;
}
if (o instanceof Person){
Person p = (Person) o;
return Objects.equals(p.name,this.name) && p.age == this.age; }
return false;
}
}

Main.java

public class Main {
public static void main(String[] args) throws IOException {
Queue<Person> queue = new LinkedList<>();
queue.offer(new Person("小明",12));
queue.offer(new Person("小红",12));
queue.offer(new Person("小军",12));
System.out.println(queue.poll());
System.out.println(queue.poll());
System.out.println(queue.poll());
System.out.println(queue.poll()); }
}


```#java
public class Main {
public static void main(String[] args) throws IOException {
Queue queue = new LinkedList();
queue.offer(new Person("小明",12));
queue.offer(new Person("小红",12));
queue.offer(new Person("小军",12));
System.out.println(queue.poll());
System.out.println(queue.poll());
System.out.println(queue.poll());
System.out.println(queue.remove());
}
}
```

```#java
public class Main {
public static void main(String[] args) throws IOException {
Queue queue = new LinkedList();
queue.offer(new Person("小明",12));
queue.offer(new Person("小红",12));
queue.offer(new Person("小军",12));
System.out.println(queue.poll());
System.out.println(queue.poll());
System.out.println(queue.poll());
//改写代码,先判断队列是否为空
if (!queue.isEmpty()) {
System.out.println(queue.remove());
}
}
}
```

总结

  • Queue实现一个FIFO的队列
  • add/offer将元素添加到队尾
  • remove/poll从队首获取元素并删除
  • element/peek从队首获取元素但不删除
  • 避免把null添加到队列

廖雪峰Java5Java集合-5Queue-1使用Queue的更多相关文章

  1. 廖雪峰Java5集合-3Map-1使用Map

    廖雪峰的视频不全,以下是疯狂Java关于Map的讲解 1.Map定义 Map是一种键值映射表,可以通过key快速查找value,同python的dict.key不允许重复,value可以重复. Map ...

  2. 廖雪峰Java5集合-4Set-1使用Set

    集合 Set用于存储不重复的元素集合: boolean add(E e) boolean remove(Object o) boolean contains(Object o) int size() ...

  3. 廖雪峰Java5集合-1Java集合简介-1Java结合简介

    1.集合 定义:集合就是一堆东西.集合里的东西,称为元素Element 数学中的集合: 有限集合: * 一个班所有的学生组成的集合 * 一个网站所有的商品组成的集合 无限集合: * 全体自然数集合 * ...

  4. 廖雪峰Java5集合-6Stack-1使用Stack

    1.栈的定义 栈Stack是一种后进先出(LIFO: Last In First Out)的数据结构,可以看作一端封闭的容器,先进去的元素永远在底部,最后出来. 栈有2个重要的方法: push(E e ...

  5. 廖雪峰Java5集合-3Map-Properties的使用

    Properties用于读取配置 properties文件只能使用ASCII码 #表示注释 可以从文件系统读取.properties文件 Properties props = new Properti ...

  6. 廖雪峰Java5集合-2List-2编写equals方法

    List是一种有序链表: List内部按照放入元素的先后顺序存放 每个元素都可以通过索引确定自己的位置 boolean contains(Object o) 是否包含某个元素 int indexOf( ...

  7. 廖雪峰Java5集合-2List-1使用List

    1.List定义 List是一种有序链表: List内部按照元素的先后顺序存放 每个元素都可以通过索引确定自己的位置 类似数组,但大小可变 //List<E>是一种有序链表: //* Li ...

  8. 爬取廖雪峰的python3教程

    从廖雪峰老师的python教程入门的,最近在看python爬虫,入手了一下 代码比较low,没有用到多线程和ip代理池 然后呢,由于robots.txt的限定,构建了一些user-agent,并放慢的 ...

  9. 廖雪峰Java6 IO编程-2input和output-4Filter模式

    1.JDK提供的InputStream分为两类: 直接提供数据的InputStream * FileInputStream:从文件读取 * ServletInputStream:从HTTP请求读取数据 ...

随机推荐

  1. 《DSP using MATLAB》Problem 6.23

    代码: %% ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ %% Output In ...

  2. url中携带中文乱码问题

    1.问题描述 在项目中碰见url中经常传递中文参数时,容易出现乱码问题,现在就这个问题产生原因和解决的措施大概分析一下,希望过路人和我能引以为戒. 2.问题分析 由于我们利用URL传递参数这种方式是依 ...

  3. Java基础四(switch、数组、)

    1.流程控制语句switch2.数组3.随机点名器案例 ###01switch语句解构 * A:switch语句解构 * a:switch只能针对某个表达式的值作出判断,从而决定程序执行哪一段代码. ...

  4. Modern Data Lake with Minio : Part 2

    转自: https://blog.minio.io/modern-data-lake-with-minio-part-2-f24fb5f82424 In the first part of this ...

  5. auto_ptr & share_ptr & unique_ptr

    Using auto_ptr, you don’t need think about the memory deallocation, but there are also many argument ...

  6. STDOUT/STDERR重定向到ALOG中

    版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/kangear/article/details/24534707      说下背景:如今众多Andr ...

  7. NET设计模式 第二部分 结构性模式(10):组合模式(Composite Pattern)

    组合模式(Composite Pattern) ——.NET设计模式系列之十一 Terrylee,2006年3月 概述 组合模式有时候又叫做部分-整体模式,它使我们树型结构的问题中,模糊了简单元素和复 ...

  8. Qt 中的事件处理(一)

    1.图形界面应用程序的消息处理模型 特点: 基于操作系统才能运行 GUI应用程序提供的功能必须由用户触发 用户操作界面时操作系统是第一个感知的 系统内核的消息通过事件处理转变成QT的信号 2. Qt中 ...

  9. Vivado约束文件(XDC)的探究(2)

    Vivado约束文件(XDC)的探究(2)

  10. Sublime Text3安装以及初次配置

    Sublime Text3安装以及初次配置 工具:官网下载:Sublime Text3 安装:直接运行安装.http://write.blog.csdn.net/postedit 激活:参考文/晚晴幽 ...