Collection
layout: default
title: Collection Superinterface
Collection is the parent interface of Set and List.
* Set describes a group of unique elements.
* List describes an ordered group of elements.

These interfaces are part of the Java collections framework.
Java collections framework¶
Interfaces and classes related to collections make up the Java Collections Framework.
A collection is an object that contains other objects. * The size of a collection can be altered after creation. * Some collection types order elements, while others do not.
Many of the Set and List methods are declared in Collection.
* boolean add(E)
* boolean contains(Object)
* Iterator<E> iterator() - List has this, too.
* boolean remove(Object)
* int size()
One very useful method that all Collections have is addAll.
* boolean addAll(Collection) - adds all elements from the input Collection to this Collection.
* Returns true if this collection (the one the method is being invoked on) changed as a result of the call.
Drill¶
SetInterface/com.example.setinterface.drills.CollectionTestIn this drill you will be using a
Collectionvariable with aListandSetimplementation class to see howaddAll()works. * Follow the instructions listed inCollectionTest.java.
Practice Exercise¶
If you want to remove duplicates from a
List, give it to aHashSetthrough eitheraddAll(Collection)or theHashSet's one-arg constructor,HashSet(Collection).