The Collections Framework (or Data
How you organize data in your classes depends on the application:
- Do you need fast access to an element at a specified
- Do you need to be able to insert and delete elements
quickly in the middle of a list?
The answers to these types of questions determines the type of
collection you should use.
Fundamental interface: Collection<E>
The Set<E>, List<E>, and Queue<E> interfaces are all
subinterfaces of the Collection interface. The Collection interface
contains methods that apply to all types of sets, lists and queues.
The Map<E> interface is part of the Collections Framework, but
does not inherit from Collection.
- set - an unordered collection of elements which does
not contain duplicates.
- list - an ordered collection of elements which are
indexed by integers.
- map - an unordered collection of key-value pairs.
- queue - an ordered collection in which new elements are
added and removed from only one end of the list.
Two Basic Methods from the Collection
- boolean add(E item)
- Iterator<E> iterator()
There are others that we will talk about more later.
The add() method
This method adds an object to the collection - it returns true if
adding the object actually changes the collection, and false otherwise.
The iterator() method
This method returns an object that implements the Iterator interface.
You use the iterator object to visit the elements in the collection,
one at a time.
The Iterator Interface
method - returns the next element in the collection if there is one.
method - returns true if there are more elements left to visit.
method - removes the last element that was returned by next(). Only one
call to remove() is permitted before next() is called again. Only use
this method to delete collection elements while iterating through the
Note: If you call next() on a
collection, and you have reached the end of the collection, next()
throws a NoSuchElementException. Call hasNext() before calling next().
Example: To look at all
elements in a collection, request an interator and keep calling next()
as there are more elements in the collection.
c = new ArrayList<String>();
iter = c.iterator();
String str = iter.next();
// do something with str - remove it if it starts with a particular
Advancing an iterator - think
of an iterator as being between elements. When next() is called, the
iterator jumps over the next element, and returns a reference to the
element it jumped over.
Example: Remove the first
element in a collection
c = new ArrayList<Circle>();
... // add to
it = c.iterator();
// I saw the first element
remove the first element
Example: What happens when I
run this code?
c = new ArrayList<Circle>();
...// add 10
elements to the collection
it = c.iterator();
Another Example: A First Look at a Generic
- A method can have a type parameter just like a class.
Example: Write a
generic method addEm() that adds all objects in one collection to
<E> boolean addEm(Collection<E> to, Collection<E>
... // the
return value should indicate whether or not the collection has changed
as a result of the method call.
... // finish
writing this method, and then write code that calls it
- A very similar method called addAll() is
part of the Java library
- It's not part of the Collection interface, since an
interface cannot contain implemented methods
- Since methods like addAll() will be useful to many
collection classes, it would be useful to have the implementation for
this and other routine methods available.
- Collection cannot provide an implementation since it is
- So the abstract class AbstractCollection implements
- this class leaves the fundamental methods add() and
iterator() as abstract methods
- implements other methods, like addAll(), in terms of
those fundamental methods
- If you want to write a concrete Collection class, you
probably want to extend AbstractCollection rather than implementing the
- public abstract class AbstractCollection<E>
- To create your own collection class by extending
AbstractCollection, you must implement size() and iterator(), and
possibly other optional methods.
the Collection Interface
iterator() - returns an iterator that can be used to visit the
elements in the Collection
- returns number of elements in the collection
isEmpty() - returns true if collection contains no elements
contains(Object o) - returns true if this collection contains an
object equal to o
containsAll(Collection<?> other) - returns true if
this collection contains all elements in other
-- The type Collection<?> is called a wildcard
type, and is the super type of all Collection types.
-- The type of its members match any element type.
obj) - adds obj to collection, and returns true if collection
changes as a result of the call
addAll(Collection<? extends E> other) - adds all elements
of other collection to this collection. Returns true if this collection
is changed as a result of the call.
-- Collection<? extends E> is a bounded wildcard.
-- ? stands for an unknown type that inherits from
-- E is called the upper bound of the wildcard.
remove(Object obj) - removes an object equal to obj from this
collection if such an object is in the collection. Returns true if a
matching object was removed.
removeAll(Collection<?> other) - removes all elements in
other from this collection. Returns true if this collection was
modified by the call.
- removes all elements from this collection.
retainAll(Collection<?> other) - removes all elements
from this collection that are not equal to an element in other. Returns
true if this collection was changed by the call.
toArray(T arr) - returns an array containing the elements in
this collection. If the specified array arr is large enough to hold the
elements, then the elements are stored in arr (and any unused entries
in arr are set to null). Otherwise a new array containing this
collection's elements is returned.
Note about constructors in Collection
implementations: A class that implements Collection must have a
constructor that takes a Collection argument. This constructor
initializes the new Collection to contain all elements in the specified
Example: Suppose you have a
collection c of Strings (which could be a List, Set, etc). To create a
new ArrayList containing all elements of c.
l = new ArrayList<String>(c);
Recall the Collection methods:
Determine number of elements in
Determine if an object is in
Add or remove an
Visit elements in