用ArrayDeque比用LinkedList更优于实现Deque?

问题诱因:https://leetcode.com/submissions/detail/481019484/中,Deque<String> l1 = new ArrayDeque<>(Arrays.asList(sentence1.split(" "))); 用到了ArrayDeque而不是LinkedList。为什么?

LinkedListArrayDeque
数据结构带头尾结点的双向链表长度为2的幂的环形数组1
读取效率Most ArrayDeque operations run in amortized constant time. Exceptions include remove, removeFirstOccurrence, removeLastOccurrence, contains, iterator.remove(), and the bulk operations, all of which run in linear time.2
元素可否为null不可

题目中,对Deque的操作主要是去除头尾元素。因为ArrayDeque用的是环形数组,所以即使是读/操作数组头,也不会像ArrayList那样引发其它数组元素的移动,即时间复杂度是O(1)而非O(n)。所以在此题中,即使从效率考虑,可以使用ArrayDeque。在效率方面,LinkedList优于ArrayDeque的唯一操作,应该是随机删。相同观点可见于https://stackoverflow.com/a/6163204/14993533


  1. 源码中pollFirst()方法中的elements[h] = null; head = (h + 1) & (elements.length - 1);可为佐证 ↩︎

  2. from jdk1.8官方文档 ↩︎

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

qq_23204557

你的鼓励将是我创作的最大动力

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

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

打赏作者

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

抵扣说明:

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

余额充值