Skip to content

Commit 3428b7e

Browse files
committed
Add note about Queue and Dqueue in java collection
1 parent 64f324a commit 3428b7e

1 file changed

Lines changed: 28 additions & 6 deletions

File tree

java/basic/java-collection.md

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -391,12 +391,13 @@ E set(int index, E element) // 替换指定位置的元素
391391

392392
LinkedList可以作为FIFO的队列,使用如下方法:
393393
```java
394-
add(e)
395-
offer(e)
396-
remove()
397-
poll()
398-
element()
399-
peek()
394+
// Queue
395+
boolean add(e) // add比offer的区别在于add时如果capacity超了,会报错,而offer不会
396+
boolean offer(e)
397+
E remove() // 返回队列的第一个元素,并且删除,如果队列为空,报NoSuchElementException
398+
E poll() // 返回队列的第一个元素,并且删除,如果队列为空,返回null
399+
E element() // 返回队列的第一个元素,不删除,如果队列为空,报NoSuchElementException
400+
E peek() // 返回队列的第一个元素,不删除,如果队列为空,返回null
400401
```
401402

402403
LinkedList也可以作为FILO的栈,使用如下方法:
@@ -740,8 +741,29 @@ TreeSet实现了NavigableSet接口,因此其支持集合的导航方法,如l
740741
```
741742

742743
### Queue接口源码解析
744+
745+
`源码分析`
746+
```java
747+
public interface Queue<E> extends Collection<E> {
748+
boolean add(e) // add比offer的区别在于add时如果capacity超了,会报错,而offer不会
749+
boolean offer(e)
750+
E remove() // 返回队列的第一个元素,并且删除,如果队列为空,报NoSuchElementException
751+
E poll() // 返回队列的第一个元素,并且删除,如果队列为空,返回null
752+
E element() // 返回队列的第一个元素,不删除,如果队列为空,报NoSuchElementException
753+
E peek() // 返回队列的第一个元素,不删除,如果队列为空,返回null
754+
}
755+
```
756+
743757
### Deque接口源码解析
758+
759+
`总结`
760+
```
761+
双向队列,继承自Queue接口,同时提供了丰富的操作队列的方法,具体可参考LinkedList源码分析
762+
```
763+
744764
##### LinkedList使用
765+
Go to here [LinkedList源码解析和使用](#LinkedList源码解析和使用)
766+
745767
## Map集合下常用实现类详解
746768
#### AbstractMap接口源码解析
747769
##### HashMap源码解析和使用

0 commit comments

Comments
 (0)