Collections Framework
What we’ll cover
What is a collection?
The Collection Interface
The Iterator Interface
The Iterable Interface
Iterator and Collection interface
What is a Collection?
Collectionis an interface which ensures a class has the ability to hold a series of objects.- Often, we consider
Mapobjects to be aCollection, although they do not implement theCollectioninterface.
Collections and Maps
| List | Set | Map |
|---|---|---|
| ArrayList | HashSet | TreeMap |
| LinkedList | TreeSet | HashMap |
| ArrayDeque | PriorityQueue | WeakHashMap |
| EnumSet | IdentityHashMap | |
| LinkedHashSet | LinkedHashMap | |
Collection Interface
- Fundamental interface for
Collectionclasses in java.
public interface Collection<E> extends Iterable<E> {
boolean add(E element);
boolean addAll(Collection<? extends E> collection);
void clear();
boolean contains(Object object);
boolean containsAll(Collection<?> collection);
boolean isEmpty();
Iterator<E> iterator();
boolean remove(Object object);
boolean removeAll(Collection<?> collection);
boolean retainAll(Collection<?> collection);
int size();
Object[] toArray();
<T> T[] toArray(T[] array);
}
Collection Interface
boolean add(E element)
- Attempts to add an element to the
Collection - returns
trueif adding the element changes theCollection, elsefalse. - Adding an already-present-object to a
Setcollection will returnfalse.
public void demo() {
Collection<String> set = new HashSet<>();
String valueToBeAdded = "Hedjet";
set.add(valueToBeAdded);
System.out.println(set.add(valueToBeAdded)); // prints false
}
Collection Interface
boolean addAll(Collection)
- Attempts to add a collection of elements to the
Collection - returns
trueif adding the elements changes theCollection, elsefalse.
public void demo() {
Collection<String> set = new HashSet<>();
String[] valuesToBeAdded = {"Froilan", "Wilhem", "Leon", "Nhu", "Kris"};
Collection<String> valuesAsList = Arrays.asList(valueToBeAdded);
System.out.println(set.addAll(list)); // prints true
}
Collection Interface
boolean remove(Object)
- Attempts to remove an object from the
Collection - returns
trueif removing the element changes theCollection, else returnsfalse - Removing an element that is not present in an
ArrayListwill returnfalse.
public void demo() {
// prints false
System.out.println(new ArrayList().remove(new Object()));
}
Collection Interface
boolean removeAll(Collection)
- Attempts to remove a collection of elements from the
Collection - returns
trueif removing the elements changes theCollection, else returnsfalse
public void demo() {
String[] elementsAsArray = {"The", "Quick", "Brown"};
Collection<String> originalCollection = new ArrayList<>();
Collection<String> elementsAsList = Arrays.asList(elementsAsArray);
// prints false
System.out.println(originalCollection.removeAll(elementsAsList));
}
Collection Interface
boolean retainAll(Collection)
- Retains only the elements in this collection that are contained in the specified collection.
- returns
trueif retaining the elements changes theCollection, else returnsfalse
public void demo() {
String[] originalArray = {"The", "Quick", "Brown"};
String[] elementsToBeRetained = {"The", "Quick"};
List<String> originalList = new ArrayList<>(Arrays.asList(originalArray));
List<String> retentionList = Arrays.asList(elementsToBeRetained);
// prints true
System.out.println(originalList.retainAll(retentionList)));
}
Collection Interface
boolean isEmpty()
- returns
trueif the size of theCollectionis0, else returnsfalse.
public void demo() {
String[] elementsAsArray = {"The", "Quick", "Brown"};
Collection<String> elementsAsList = Arrays.asList(arrayOfStrings);
System.out.println(elementsAsList.isEmpty()); // prints false
}
Collection Interface
int size()
- Returns the number of elements in the
Collection.
public void demo() {
String[] elementsAsArray = {"The", "Quick", "Brown"};
Collection<String> elementsAsList = Arrays.asList(arrayOfStrings);
System.out.println(elementsAsList.size()); // prints 3
}
Collection Interface
void clear()
- Removes all elements from the
Collection.
public void demo() {
String[] elementsAsArray = {"The", "Quick", "Brown"};
List<String> elementsAsList = new ArrayList<>(Arrays.asList(arrayOfStrings));
elementsAsList.clear();
System.out.println(elementsAsList.isEmpty()); // prints true
}
Collection Interface
Object[] toArray()
- Populates a new
Object[]with the elements from thisCollection
public void demo() {
String[] elementsToAdd = {"The", "Quick", "Brown"};
List<String> elementList = new ArrayList<>(Arrays.asList(elementsToAdd));
Object[] listAsObjectArray = elementList.toArray();
}
Collection Interface
E[] toArray(E[])
- Populates a new array of the respective type with the elements from this
Collection
public void demo() {
String[] elementsToAdd = {"The", "Quick", "Brown"};
List<String> elementList = new ArrayList<>(Arrays.asList(elementsToAdd));
int newArrayLength = elementList.size();
String[] arrayToBePopulated = new String[newArrayLength];
String[] listAsStringArray = elementList.toArray(arrayToBePopulated);
}
Iterator interface
Iteratoris used to visit the elements in theCollection, one by one.
public interface Iterator<E> {
E next();
boolean hasNext();
void remove();
default void forEachRemaining(Consumer<? super E> action);
}
Collection Interface
Iterator<E> iterator()
- Returns an object that implements the
Iteratorinterface
Iterable Interface
Iterableensures the implementing class is a valid candidate for theforeachloopCollectionextendsIterable, therefore allCollectiontypes are valid candidates for theforeachloop.- All
Iterables must provide an implementation forIterator<E> iterator(). - Is NOT the same as the
Iteratorinterface.
Iterable Interface
public interface Iterator<E> {
Iterator<E> iterator();
forEach(Consumer<? super E> E);
}
Iterator Interface
- Repeatedly calling the
next()method enables you to visit each element from the collection, one by one. NoSuchElementExceptionis thrown upon invokingnext()on anIteratorthat has reached the end of the collection.- This can be prevented by evaluating the
hasNext()method before callingnext(). - The compiler translates
foreachloops into a loop with an iterator.
- This can be prevented by evaluating the
public static void printIterable(Iterable<Object> iterable) {
Iterator iterator = iterable.iterator();
while(iterator.hasNext()) {
System.out.println("Current Element = " + iterator.next());
}
}
Iterator Interface
- As of Java8, you can call the
forEachRemainingmethod with aConsumerlambda expression. - The lambda expression is invoked with each element of the iterator, until there are none left.
public static void printIterable(Iterable<Object> iterable) {
Iterator iterator = iterable.iterator();
iterator.forEachRemaining((element) -> System.out.println(element));
}
Iterator Interface
next()
- Think of Java iterators as being between elements.
- When you call
next, the iterator jumps over the next element, and it returns a reference to the element that it just passed.
Iterator Interface
remove()
- removes the element that was returned by the last call to
next() - Often, you may need to view an element before deciding to delete it.
- It is illegal to call
remove()if it wasn’t preceded by a call tonext().
public void deleteFirstElement(Iterator<String> iterator) {
iterator.next(); // skip first element
iterator.remove(); // remove first element
}
AbstractCollection Class
- The
Collectioninterface declares 18 methods. - To avoid implementing a lot of the fundamental methods, the
Collectionlibrary developers created anAbstractCollectionclass. - AbstractCollection has a concrete implementation of all
Collectionmethods exceptsize()anditerator()
Sample AbstractCollection implementation
public class MyCollection<E> extends AbstractCollection<E> {
private final Iterable<E> iterable;
public MyCollection(Iterable<E> iterable) {
this.iterable = iterable;
}
@Override
public Iterator<E> iterator() { return iterable.iterator(); }
@Override
public int size() {
List<E> list = new ArrayList<>();
iterator().forEachRemaining(list::add);
return list.size();
}
}

