LinkedList 除了與 ArrayList 提供的鏈表常用的方法之外,還提供了對(duì)棧,隊(duì)列,以及雙端隊(duì)列的支持。所以 LinkedList 的方法比 ArrayList 豐富,并且將 LinkedList 向上轉(zhuǎn)型為 List 時(shí),這些這棧,隊(duì)列,雙端隊(duì)列支持的方法也將不能使用。
Queue 接口 繼承了 Collection 接口,并在 COllection 接口的基礎(chǔ)上擴(kuò)展了對(duì)容器元素的插入,提取,以及檢查操作。每種操作都提供了兩種不同的方法,當(dāng)相應(yīng)的操作失敗時(shí),一種拋出異常而另一種返回一個(gè)特殊值(null 或者 false)。插入失敗的接口設(shè)計(jì)針對(duì)的是容量大小有限制的隊(duì)列,一般情況下不會(huì)失敗。
拋出異常 | 返回特殊值 |
---|---|
add(e) | offer(e) |
remove() | poll() |
element() | peek() |
在隊(duì)列的使用中,不應(yīng)該插入 null 值,盡管它的實(shí)現(xiàn) LinkedList 允許插入,但是也不應(yīng)該這樣做,應(yīng)為隊(duì)列 poll() 使用 null 作為隊(duì)列為空的標(biāo)志。
拋出異常 | 返回特殊值 |
---|---|
addFirst(e)/addLast(e) | offerFirst(e)/offerLast(e) |
removeFirst()/removeLast() | pollFirst()/pollLast() |
getFirst()/getLast() | peekFirst()/peekLast() |
queue和deque 方法對(duì)比
queue | deque |
---|---|
add(e) | addFirst(e) |
offer(e) | offerFirst(e) |
remove() | removeFirst() |
poll() | pollFirst() |
element() | getFirst() |
peek() | peekFirst() |
雙端隊(duì)列的接口設(shè)計(jì)時(shí)提供了對(duì)棧的支持,即可以將雙端隊(duì)列作為棧來(lái)操作。而這種用法要優(yōu)于舊的 Stack 類。
stack | deque |
---|---|
push(e) | addFirst(e) |
pop() | removeFirst() |
peek() | peekFirst() |
在優(yōu)先隊(duì)列上調(diào)用 offer() 方法插入對(duì)象時(shí),這個(gè)對(duì)象在隊(duì)列內(nèi)會(huì)被排序。自己定義的類需要實(shí)現(xiàn) comparable 或者提供自動(dòng)的比較器。
Map 通過(guò) keySet(),values() 可以返回 COllection ,從而與 Collection 建立聯(lián)系。
Collection 可以直接使用 foreach 語(yǔ)法,但是 Iterator 不能。但是兩者各有優(yōu)缺點(diǎn)。Collection 接口可以通過(guò)繼承 AbstractColleciton 類因此有多繼承的限制,而使用 Iterator 只需要在類定義一個(gè)返回該接口實(shí)例的方法。
public class MyCollection extends AbstractCollection<String> { private String[] array; public MyCollection(){ super(); array = new String[]{"red","blue","black"}; } public Iterator<String> iterator() { return new Iterator<String>() { private int index = 0; public boolean hasNext() { return index < size(); } public String next() { return array[index++]; } }; } public int size() { return array.length; } public static void main(String[] args){ MyCollection myCollection = new MyCollection(); CollectionTest.display(myCollection); }}public class MyCollection { private String[] array; public MyCollection(){ super(); array = new String[]{"red","blue","black"}; } public Iterator<String> iterator() { return new Iterator<String>() { private int index = 0; public boolean hasNext() { return index < size(); } public String next() { return array[index++]; } }; } public int size() { return array.length; } public static void main(String[] args){ MyCollection myCollection = new MyCollection(); CollectionTest.display(myCollection.iterator()); }}Iterable 是 java SE5 之后添加的新接口,任何實(shí)現(xiàn)該接口的類都可以使用 foreach 語(yǔ)句,Colleciton 接口繼承了 Iterable 接口。但是數(shù)組雖然可以使用 foreach,但是數(shù)組并沒(méi)有實(shí)現(xiàn) Iterable 接口。public class MyCollection implements Iterable<String> { private String[] array; public MyCollection(){ super(); array = new String[]{"red","blue","black"}; } public Iterator<String> iterator() { return new Iterator<String>() { private int index = 0; public boolean hasNext() { return index < size(); } public String next() { return array[index++]; } }; } public int size() { return array.length; } public static void main(String[] args){ MyCollection myCollection = new MyCollection(); for(String color : myCollection){ System.out.print(color + " "); } }}使用適配器模式顯式的調(diào)用 foreach 語(yǔ)法。通過(guò)上面的例子可以知道,當(dāng)需要支持 foreach 語(yǔ)法時(shí)可以實(shí)現(xiàn) Iterable 接口。但是假設(shè)我們希望有多個(gè)不同方式來(lái)進(jìn)行迭代,這時(shí)可以采用適配器模式。
public Iterable<String> reversed(){ return new Iterable<String>() { public Iterator<String> iterator() { return new Iterator<String>() { private int i = size(); public boolean hasNext() { return i > 0; } public String next() { return array[--i]; } }; } };}MyCollection myCollection = new MyCollection();for(String color : myCollection){ System.out.print(color + " ");}System.out.println();for(String color : myCollection.reversed()){ System.out.print(color + " ");}新聞熱點(diǎn)
疑難解答
圖片精選
網(wǎng)友關(guān)注