LinkedList 除了与 ArrayList 提供的链表常用的方法之外,还提供了对栈,队列,以及双端队列的支持。所以 LinkedList 的方法比 ArrayList 丰富,并且将 LinkedList 向上转型为 List 时,这些这栈,队列,双端队列支持的方法也将不能使用。
Queue 接口 继承了 Collection 接口,并在 COllection 接口的基础上扩展了对容器元素的插入,提取,以及检查操作。每种操作都提供了两种不同的方法,当相应的操作失败时,一种抛出异常而另一种返回一个特殊值(null 或者 false)。插入失败的接口设计针对的是容量大小有限制的队列,一般情况下不会失败。
抛出异常 | 返回特殊值 |
---|---|
add(e) | offer(e) |
remove() | poll() |
element() | peek() |
在队列的使用中,不应该插入 null 值,尽管它的实现 LinkedList 允许插入,但是也不应该这样做,应为队列 poll() 使用 null 作为队列为空的标志。
抛出异常 | 返回特殊值 |
---|---|
addFirst(e)/addLast(e) | offerFirst(e)/offerLast(e) |
removeFirst()/removeLast() | pollFirst()/pollLast() |
getFirst()/getLast() | peekFirst()/peekLast() |
queue和deque 方法对比
queue | deque |
---|---|
add(e) | addFirst(e) |
offer(e) | offerFirst(e) |
remove() | removeFirst() |
poll() | pollFirst() |
element() | getFirst() |
peek() | peekFirst() |
双端队列的接口设计时提供了对栈的支持,即可以将双端队列作为栈来操作。而这种用法要优于旧的 Stack 类。
stack | deque |
---|---|
push(e) | addFirst(e) |
pop() | removeFirst() |
peek() | peekFirst() |
在优先队列上调用 offer() 方法插入对象时,这个对象在队列内会被排序。自己定义的类需要实现 comparable 或者提供自动的比较器。
Map 通过 keySet(),values() 可以返回 COllection ,从而与 Collection 建立联系。
Collection 可以直接使用 foreach 语法,但是 Iterator 不能。但是两者各有优缺点。Collection 接口可以通过继承 AbstractColleciton 类因此有多继承的限制,而使用 Iterator 只需要在类定义一个返回该接口实例的方法。
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 之后添加的新接口,任何实现该接口的类都可以使用 foreach 语句,Colleciton 接口继承了 Iterable 接口。但是数组虽然可以使用 foreach,但是数组并没有实现 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 + " "); } }}使用适配器模式显式的调用 foreach 语法。通过上面的例子可以知道,当需要支持 foreach 语法时可以实现 Iterable 接口。但是假设我们希望有多个不同方式来进行迭代,这时可以采用适配器模式。
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 + " ");}新闻热点
疑难解答