java queue源码_java源码解读--queue
queue接口特點:可以模擬隊列行為,即“先進先出”。
接口結構
queue接口繼承了Collection接口,并增加了一些新方法
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16public interface extends Collection{
boolean add(E e);
//將元素插入隊列,如果失敗返回false
boolean offer(E e);
//移除并返回隊列中的第一個元素,隊列為空時,拋異常
E remove();
//移除并返回隊列中的第一個元素,隊列為空時,返回null
E poll();
//返回隊列中第一個元素,但不移除,隊列為空時,拋異常
E element();
//返回隊列中第一個元素,但不移除,對列為空時,返回null
E peek();
}
抽象類AbstractQueue
queue接口中,add與offer、remove與poll、element與peek,功能一致,只是對異常情況的處理不同。AbstractQueue源碼中可以看出這些方法處理異常的方式。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49public abstract class AbstractQueue
extends AbstractCollection
implements {
protected AbstractQueue() {
}
//add底層調用offer,當offer失敗返回false時,add方法拋出異常
public boolean add(E e) {
if (offer(e))
return true;
else
throw new IllegalStateException("Queue full");
}
//remove底層調用poll,當poll返回null時,remove方法拋出異常
public E remove() {
E x = poll();
if (x != null)
return x;
else
throw new NoSuchElementException();
}
//element底層調用peek,當peek返回null時,element方法拋出異常
public E element() {
E x = peek();
if (x != null)
return x;
else
throw new NoSuchElementException();
}
//clear方法循環調用poll,直到返回為null
public void clear() {
while (poll() != null)
;
}
public boolean addAll(Collection extends E> c) {
if (c == null)
throw new NullPointerException();
if (c == this)
throw new IllegalArgumentException();
boolean modified = false;
for (E e : c)
if (add(e))
modified = true;
return modified;
}
}
Deque接口
Deque是雙端隊列,底層由循環數組實現,其繼承了Queue接口,并擴展了新特性。
Deque重寫了Queue的全部方法,Stack、Collection的部分方法,并增加了對首尾元素處理的相關方法。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65public interface Deque extends {
void addFirst(E e);
void addLast(E e);
boolean offerFirst(E e);
boolean offerLast(E e);
E removeFirst();
E removeLast();
E pollFirst();
E pollLast();
E getFirst();
E getLast();
E peekFirst();
E peekLast();
boolean removeFirstOccurrence(Object o);
boolean removeLastOccurrence(Object o);
// *** Queue methods ***
boolean add(E e);
boolean offer(E e);
E remove();
E poll();
E element();
E peek();
// *** Stack methods ***
void push(E e);
E pop();
// *** Collection methods ***
boolean remove(Object o);
boolean contains(Object o);
public int size();
Iterator iterator();
Iterator descendingIterator();
}
ArrayDeque
ArrayDeque不可以存取null元素,因為會根據某個位置是否為null來判斷元素是否存在。
當作為棧使用時,性能比stack好,作為隊列使用時,性能比linkedlist好
類定義
繼承了AbstractCollection,實現了Deque接口
1
2public class ArrayDeque extends AbstractCollection
implements Deque, Cloneable, Serializable
重要的成員變量1
2
3
4// 第一個元素的索引
private transient int head;
// 下個要添加元素的位置,為末尾元素的索引 + 1
private transient int tail;
構造方法1
2
3
4
5
6
7
8
9
10
11
12
13
14//默認數組大小16
public ArrayDeque() {
elements = new Object[16];
}
//傳入數組大小
public ArrayDeque(int numElements) {
//調整實際數組大小為大于傳入值的最小的2的冪次
allocateElements(numElements);
}
//直接傳入集合
public ArrayDeque(Collection extends E> c) {
allocateElements(c.size());
addAll(c);
}
調整數組大小1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17private void allocateElements(int numElements) {
int initialCapacity = MIN_INITIAL_CAPACITY;
// 找到大于需要長度的最小的2的冪整數。
if (numElements >= initialCapacity) {
initialCapacity = numElements;
initialCapacity |= (initialCapacity >>> 1);
initialCapacity |= (initialCapacity >>> 2);
initialCapacity |= (initialCapacity >>> 4);
initialCapacity |= (initialCapacity >>> 8);
initialCapacity |= (initialCapacity >>> 16);
initialCapacity++;
if (initialCapacity < 0) // Too many elements, must back off
initialCapacity >>>= 1;// Good luck allocating 2 ^ 30 elements
}
elements = (E[]) new Object[initialCapacity];
}
add1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34public void addFirst(E e) {
if (e == null)
throw new NullPointerException();
//因為elements.length是2的冪次,(head - 1) & (elements.length - 1)相當于求模操作
//head-1只可能為-1,-1&(elements.length - 1)=(elements.length - 1)
elements[head = (head - 1) & (elements.length - 1)] = e;
if (head == tail)
doubleCapacity();
}
public void addLast(E e) {
if (e == null)
throw new NullPointerException();
elements[tail] = e;
//與head操作類似,為了處理tail=length-1的情況
if ( (tail = (tail + 1) & (elements.length - 1)) == head)
doubleCapacity();
}
public boolean add(E e) {
addLast(e);
return true;
}
public boolean offerFirst(E e) {
addFirst(e);
return true;
}
public boolean offerLast(E e) {
addLast(e);
return true;
}
remove1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40public E removeFirst() {
E x = pollFirst();
if (x == null)
throw new NoSuchElementException();
return x;
}
public E removeLast() {
E x = pollLast();
if (x == null)
throw new NoSuchElementException();
return x;
}
public E pollFirst() {
int h = head;
("unchecked")
E result = (E) elements[h];
// Element is null if deque empty
if (result == null)
return null;
//刪除頭元素
elements[h] = null; // Must null out slot
//更改head下標,處理越界情況讓head=0
head = (h + 1) & (elements.length - 1);
return result;
}
public E pollLast() {
//獲取tail下標,處理tail=0的特殊情況,移除元素后tail=elements.length-1
int t = (tail - 1) & (elements.length - 1);
("unchecked")
E result = (E) elements[t];
if (result == null)
return null;
elements[t] = null;
//tail指向下個要添加元素的索引
tail = t;
return result;
}
刪除指定元素
需要遍歷數組,時間復雜度較高
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33public boolean removeFirstOccurrence(Object o) {
if (o == null)
return false;
int mask = elements.length - 1;
int i = head;
Object x;
while ( (x = elements[i]) != null) {
if (o.equals(x)) {
delete(i);
return true;
}
i = (i + 1) & mask;
}
return false;
}
public boolean removeLastOccurrence(Object o) {
if (o == null)
return false;
int mask = elements.length - 1;
//末尾元素的索引
int i = (tail - 1) & mask;
Object x;
while ( (x = elements[i]) != null) {
if (o.equals(x)) {
delete(i);
return true;
}
//從尾到頭遍歷
i = (i - 1) & mask;
}
return false;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44private boolean delete(int i) {
//檢查有效性
checkInvariants();
final Object[] elements = this.elements;
final int mask = elements.length - 1;
final int h = head;
final int t = tail;
//i前面的元素
final int front = (i - h) & mask;
//i后面的元素
final int back = (t - i) & mask;
//i不在t和h之間
if (front >= ((t - h) & mask))
throw new ConcurrentModificationException();
// Optimize for least element motion
if (front < back) {
// head X X i X X X X tail,head-i的元素大于i-tail元素個數
if (h <= i) {
System.arraycopy(elements, h, elements, h + 1, front);
} else { // Wrap around
// i X X X X tail X X head
System.arraycopy(elements, 0, elements, 1, i);
elements[0] = elements[mask];
System.arraycopy(elements, h, elements, h + 1, mask - h);
}
elements[h] = null;
head = (h + 1) & mask;
return false;
} else {
// head X X X X i X X tail
if (i < t) { // Copy the null tail as well
System.arraycopy(elements, i + 1, elements, i, back);
tail = t - 1;
} else { // Wrap around
// tail X X head X X X X i
System.arraycopy(elements, i + 1, elements, i, mask - i);
elements[mask] = elements[0];
System.arraycopy(elements, 1, elements, 0, t);
tail = (t - 1) & mask;
}
return true;
}
1
2
3
4
5
6
7
8
9
10private void checkInvariants() {
//tail沒有元素
assert elements[tail] == null;
//head和tail位置重合,隊列為空,否則head、tail-1位置有元素
assert head == tail ? elements[head] == null :
(elements[head] != null &&
elements[(tail - 1) & (elements.length - 1)] != null);
//head-1的位置沒有元素。
assert elements[(head - 1) & (elements.length - 1)] == null;
}
擴容1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18private void doubleCapacity() {
//tail和head重合的時候進行擴容,此時tail在head左側
assert head == tail;
int p = head;
int n = elements.length;
int r = n - p; // number of elements to the right of p
int newCapacity = n << 1;
if (newCapacity < 0)
throw new IllegalStateException("Sorry, deque too big");
Object[] a = new Object[newCapacity];
//先復制head到element數組末尾的元素
System.arraycopy(elements, p, a, 0, r);
//在復制0到tail之間的元素
System.arraycopy(elements, 0, a, r, p);
elements = a;
head = 0;
tail = n;
}
標簽:head,elements,java,int,queue,tail,源碼,return,null
來源: https://www.cnblogs.com/petewell/p/11588764.html
總結
以上是生活随笔為你收集整理的java queue源码_java源码解读--queue的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: java有效索引范围_Java索引超出范
- 下一篇: java 接口防刷_java轻量级接口限