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. BinarySearch(Java)

    private int binarySearch(int[] input, int target) { if (input == null) { return -1; } int index1 = 0 ...

  2. rest-framework之频率控制

    rest-framework之频率控制 本文目录 一 频率简介 二 自定义频率类,自定义频率规则 三 内置频率类及局部使用 四 内置频率类及全局使用 五 源码分析 回到目录 一 频率简介 为了控制用户 ...

  3. 前端tab切换 和 validatejs表单验证插件

    一.tab切换 <!DOCTYPE html> <html lang="en"> <head> <meta charset="U ...

  4. 《DSP using MATLAB》Problem 5.17

    1.代码 %% ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ %% ...

  5. hdu1542 Atlantis 线段树--扫描线求面积并

    There are several ancient Greek texts that contain descriptions of the fabled island Atlantis. Some ...

  6. 4.input()

    >>> help(input) Help on built-in function input in module builtins: input(prompt=None, /) R ...

  7. centos7设置grub密码保护

    设置grub密码保护:查看grub登录用户名cat /etc/grub.d/01_users,可以看到用户名为root.通过grub2-setpasswords设置grub密码,确认密码 cat /b ...

  8. Scalable MySQL Cluster with Master-Slave Replication, ProxySQL Load Balancing and Orchestrator

    MySQL is one of the most popular open-source relational databases, used by lots of projects around t ...

  9. ML(1)——机器学习简述

    简述 机器学习是人工智能的一种实现方式:深度学习是一种实现机器学习的技术,或者说是一种特殊的机器学习方法,可以说广义上的机器学习也包括了深度学习,三者的关系如下图所示: 从判别垃圾邮件到无人驾驶技术, ...

  10. 用windbg 检查内存泄漏

    1.下载编译https://github.com/0cch/luadbg 2.编写脚本1.txt .load luadbg_v15*.sympath+ srv*c:\MyServerSymbols*h ...