Collection
interface extends the
Iterable
interface.
interface Collection<E> implements Iterable<E> { boolean add(E e)* boolean addAll(Collection<? extends E> c)* void clear()* boolean contains(Object o) boolean containsAll(Collection<?> c) boolean equals(Object o) int hashCode() boolean isEmpty() Iterator<E> iterator() boolean remove(Object o)* boolean removeAll(Collection<?> c)* boolean retainAll(Collection<?> c)* int size() Object[] toArray() <T> T[] toArray(T[] a) }
* Optional
UnsupportedOperationException
when inapplicable optional methods are called.
boolean containsAll(Collection<?> c)
s.
containsAll(c)
returns true if everything in c
is in s
.boolean removeAll(Collection<?> c)
s.
removeAll(c)
removes from s
everything in c
.boolean retainAll(Collection<?> c)
s.
retainAll(c)
removes from s
with everything not in c
.s
be [ 1 2 3 ]
.
s.containAll([ 1 3 ]) | returns true , leaves s unchanged. |
s.containAll([ 3 4 ]) | returns false , leaves s unchanged. |
s.removeAll([ 1 3 ]) | returns true , s will be [ 2 ] . |
s.removeAll([ 3 4 ]) | returns true , s will be [ 1 2 ] . |
s.removeAll([ 4 ]) | returns false , leaves s unchanged. |
s.retainAll([ 1 3 ]) | returns true , s will be [ 1 3 ] . |
s.retainAll([ 3 4 ]) | returns true , s will be [ 3 ] . |
s.retainAll([ 4 ]) | returns true , s will be [ ] . |
containsAll()
can be implemented using Collection interface methods:
boolen containsAll(Collection<?> c) for (Object o: c) if (!contains(o)) return false return true
removeAll()
and retainAll()
? Can they too be
implemented using Collection interface methods?
List
interface tries to be general over
sequences.
interface List<E> extends Collection<E> { boolean add(E e) void add(int index, E element) boolean addAll(Collection<? extends E> c) boolean addAll(int index, Collection<? extends E> c) void clear() boolean contains(Object o) boolean containsAll(Collection<?> c) boolean equals(Object o) E get(int index) int hashCode() int indexOf(Object o) boolean isEmpty() Iterator<E> iterator() int lastIndexOf(Object o) ListIterator<E> listIterator() ListIterator<E> listIterator(int index) E remove(int index) boolean remove(Object o) boolean removeAll(Collection<?> c) boolean retainAll(Collection<?> c) E set(int index, E element) int size() List<E> subList(int fromIndex, int toIndex) Object[] toArray() <T> T[] toArray(T[] a) }
set()
, for example) can be skipped.
get()
, for example) have expensive (linear)
implementations.
interface Queue<E> extends Collection<E> { boolean add(E e) boolean addAll(Collection<? extends E> c) void clear() boolean contains(Object o) boolean containsAll(Collection<?> c) E element() boolean equals(Object o) int hashCode() boolean isEmpty() Iterator<E> iterator() boolean offer(E e) E peek() E poll() E remove() boolean remove(Object o) boolean removeAll(Collection<?> c) boolean retainAll(Collection<?> c) int size() Object[] toArray() <T> T[] toArray(T[] a) }
Queue
interface extends the Collection
interface.
Throws | Returns | |||
exception | special value | |||
Insert | add(e) | offer(e) | ||
Remove | remove() | poll() | ||
Examine | element() | peek() |
Deque
interface extends the Queue
interface.
Queue
operations.
addFirst(e), removeFirst(), getFirst() offerFirst(e), pollFirst(), peekFirst() addLast(e), removeLast(), getLast() offerLast(e), pollLast(), peekLast()
public interface Iterator<E> { boolean hasNext() E next() void remove()* }
* Optional
final Iterator i = c.iterator() while (i.hasNext()) f(i.next())
next()
and previous()
methods
public interface ListIterator<E> extends Iterator<E> { void add(E e)* boolean hasNext()** boolean hasPrevious() E next()** int nextIndex() E previous() int previousIndex() void remove()*, ** void set(E e)* }
* Optional, ** From Iterator
remove()
removes the element returned by next()
or previous()
.
next()
or previous()
must be called before each call to remove()
.
final Iterator i = c.iterator() while (i.hasNext()) f(i.next())
(or similar) every time is poor design.
for (T e: c) f(e)
public interface Iterable<E> { Iterator<E> iterator() }
Collection<Integer> pending = new ArrayDeque<Integer>(); Queue<Integer> pending = new ArrayDeque<Integer>(); Deque<Integer> pending = new ArrayDeque<Integer>();
AbstractQueue
class implements the
Queue
interface.
public abstract class AbstractQueue<E> extends AbstractCollection<E> implements Queue<E>
PriorityQueue
extends AbstractQueue
.
public class PriorityQueue<E> extends AbstractQueue<E> implementsSerializable
LinkedList
class extends the AbstractSequentialList
class.
AbstractSequentialList
class assumes a dynamic
link implementation.
ArrayList
class implements a linked list assuming
indexing is cheap.
ArrayList
s are more efficient than are Vector
s.
ArrayList
s aren’t thread safe and Vector
s are.
RandomAccess
interface indicates efficient
indexing.
for (int i = 0; i < lst.size(); i++) f(lst.get(i))
for (Iterator i = lst.iterator(); i.hasNext(); ) f(i.next())
RandomAccess
contains no methods; it just flags classes with
efficient indexing.
PriorityQueue
class stores elements in increasing
priority order (a min-priority queue).
Comparable
interface.
Comparator
implementing, overriding the element comparable, if any.
Stack
s were added to Java before the Collection
Framework (v. 1.0 vs v. 1.2).
This page last modified on 17 February 2010. |