当前位置: 首页 > news >正文

Java 中的 队列(Queue)与双端队列(Deque)

这篇笔记期初是因为在刷算法题的过程中,发现其他解题方法很多地方有采用栈或者队列来解题,我在这方面比较薄弱,特此学习记录一下。

关于队列,我的初始印象就是先进先出,但是通过学习,了解到队列还有双端队列(Deque)、优先队列(PriorityQueue)等类型,不同的队列有不同的进出规则。 

队列(Queue)

队列通常是以 FIFO (先进先出) 方式对元素进行排序,即在队头删除元素,队尾添加元素,在java代码中是继承了Collection<E>,所以也有Collection的方法,例如size(),isEmpty()等。这里只列出队列 Queue 的方法:

// 队列示例  初始化 结果为[1,2,3,4]
Queue<Integer> queue = new LinkedList<Integer>(){{add(1);add(2);add(3);add(4);}};
方法名描述示例
boolean add(E e);

成功向队尾加入元素返回true,否则抛出以下异常:

IllegalStateException – 如果队列有容量限制且队列已满无法添加

ClassCastException – 如果指定元素的类阻止将其添加到此队列

NullPointerException – 如果指定的元素为 null,并且此队列不允许空元素

IllegalArgumentException – 如果此元素的某些属性阻止将其添加到此队列

queue.add(5);

// 此时的队列结果

[1,2,3,4,5]

boolean offer(E e);

成功向队尾加入元素返回true,否则抛出以下异常:

ClassCastException – 如果指定元素的类阻止将其添加到此队列中

NullPointerException – 如果指定的元素为 null,并且此队列不允许 null 元素

IllegalArgumentException – 如果此元素的某些属性阻止将其添加到此队列中

与add(E e) 的区别:如果队列已满,offer(E e)方法会返回false, add(E e)会抛出异常

queue.add(5);

// 此时的队列结果

[1,2,3,4,5]

E remove();

删除并返回队列头的部元素

NoSuchElementException – 如果此队列为空

Integer remove = queue.remove();
System.out.println(remove);

// 输出结果为 1

E poll();

删除并返回队列头的部元素 ,如果队列为空,则返回null

与remove() 的区别: 如果队列为空,poll()会返回null,remove() 会抛出异常

Integer remove = queue.remove();
System.out.println(remove);

// 输出结果为 1

E element();

返回第一个元素

NoSuchElementException – 如果此队列为空

queue.element(); // 1
E peek();

返回第一个元素,如果队列为空,则返回null

与element() 的区别: 如果队列为空,peek()会返回null,element() 会抛出异常

queue.peek(); // 1

双端队列(Deque)

双端队列(Deque)支持在队头和队尾两端都能添加、删除元素或者获取元素,Deque包含的元素既可以是没有固定容量的,也可以是有容量限制的。由于 Deque 继承了Queue,除了Queue 的方法外,还有自身所需的添加、删除和获取的方法,且和Queue的方法高度相似。例如添加方法,在Queue 中是 boolean add(E e),在Deque 则是void addFirst(E e) 和 void addLast(E e) ,表示在队头、队尾的添加方法。

队头方法队尾方法
失败抛异常返回false或null失败抛异常返回false或null
添加void addFirst(E e);boolean offerFirst(E e);void addLast(E e);

boolean offerLast(E e);

删除

E removeFirst();

E pollFirst();

E removeLast();

E pollLast();

检查

E getFirst();

E getLast();

E peekFirst();

E peekLast();

addFirst 与 offerFirst 区别、addLast 与 offerLast 区别 :如果队列已满,addFirst、addLast 会抛出异常- IllegalStateException,offerFirst、offerLast 都会返回false。类比 Queue 的add(E e) 和offer(E e)的区别,且抛出的异常情况一致。

removeFirst与pollFirst 区别、removeLast 与 pollLast 区别: 如果队列为空,removeFirst、removeLast会抛出异常-NoSuchElementException ,pollFirst、pollLast 会返回null.

getFirst 与 peekFirst 区别、getLast与 peekLast区别:如果队列为空,getFirst、getLast会抛出异常-NoSuchElementException ,peekFirst、peekLast 会返回null.

其他方法:

 boolean removeFirstOccurrence(Object o);

删除指定的第一个元素,有则返回true,没有则返回false

ClassCastException – 如果指定元素的类与此双队列不兼容

NullPointerException – 如果指定的元素为空并且此双队列不允许空元素

// 举例 [5, 1, 2, 1, 3, 4]
Deque<Integer> deque  = new LinkedList<>(){{add(5);add(1);add(2);add(1);add(3);add(4);}};deque.removeFirstOccurrence(1)  // true 此时队列为 [5, 2, 1, 3, 4]deque.removeFirstOccurrence(7)  //false 此时队列为 [5, 2, 1, 3, 4]

boolean removeLastOccurrence(Object o);

删除指定的最后一个元素,有则返回true,没有则返回false

ClassCastException – 如果指定元素的类与此 deque 不兼容
NullPointerException – 如果指定的元素为 null 并且此 deque 不允许 null 元素

// 举例 [5, 1, 2, 1, 3, 4]
Deque<Integer> deque  = new LinkedList<>(){{add(5);add(1);add(2);add(1);add(3);add(4);}};deque.removeFirstOccurrence(1)  // true 此时队列为 [5, 1, 2, 3, 4]deque.removeFirstOccurrence(7)  //false 此时队列为 [5, 1, 2, 3, 4]

void push(E e): 向队首添加元素,此方法等同于 addFirst。如果队列已满会抛出异常-IllegalStateException

E pop():删除并返回队列的第一个元素,此方法等效于 removeFirst()。

Iterator<E> descendingIterator(): 以相反的顺序返回此 deque 中元素的迭代器。元素将按照从 last (tail) 到 first (head) 的顺序返回。返回: 此 deque 中元素的反向迭代器

其他

队列中除了两端队列(Deque),还有优先队列(PriorityQueue),优先队列中元素可以插入任意顺序的元素,但是会在获取元素的时候,按一定的顺序获取。

    PriorityQueue<Integer> priorityQueue = new PriorityQueue<>() {{add(5);add(1);add(2);add(1);add(3);add(4);}};System.out.println(priorityQueue); // [1, 1, 2, 5, 3, 4]System.out.println(priorityQueue.element()); // 1


http://www.mrgr.cn/news/59971.html

相关文章:

  • 解决:git SSL certificate problem: unable to get local issuer certificate
  • 计算机前沿技术-人工智能算法-大语言模型-最新研究进展-2024-10-21
  • Spring Boot Druid 数据库连接池入门
  • 8 个用于创建电商组件的 CSS 和 JS 代码片段
  • Python数据分析基础
  • shodan2:绕过shodan高级会员限制+metasploit批量验证漏洞
  • Host Key Verification Failed
  • 软件测试学习总结
  • 【Python】为Pandas加速(适合Pandas中级开发者)
  • PG数据库之数据类型入门
  • 【mysql】什么是当前读
  • JMeter 接口和性能测试常用函数最全解析!
  • ICP之点云特征计算
  • 只需要写几行 SQL,这个网站就搭好了?
  • shell脚本每日一练4
  • GitHub 上传项目保姆级教程
  • 【C++单调栈 贡献法】907. 子数组的最小值之和|1975
  • python基于django线上视频学习系统设计与实现_j0189d4x
  • 【Linux系统编程】——Linux入门指南:从零开始掌握操作系统的核心(指令篇)
  • 基于SpringBoot的中药材进存销管理系统设计与实现
  • 在浏览器中运行 Puppeteer:解锁新能力
  • React 中组件通信的几种主要方式
  • Python实现摇号系统
  • 还没想好说什么
  • Linux:指令再认识
  • 【在WindoWs 10 cmd查询管理目录下所有文件及其相对位置】