Java Collection Interface Methods - Cheat Sheet
What is Collection Interface?
The Collection interface is the root interface of the Java Collections Framework. It defines basic methods that
all collections like List, Set, and Queue must implement. Common classes that implement Collection include
ArrayList, HashSet, LinkedList, etc.
Located in: [Link]<E>
add(E e)
Adds an element to the collection. Returns true if the collection changed.
addAll(Collection<? extends E> c)
Adds all elements from another collection.
remove(Object o)
Removes a single instance of the specified element.
removeAll(Collection<?> c)
Removes all elements in the collection that are also in the specified collection.
retainAll(Collection<?> c)
Keeps only the elements that are also in the specified collection.
clear()
Removes all elements from the collection.
size()
Returns the number of elements.
Java Collection Interface Methods - Cheat Sheet
isEmpty()
Returns true if the collection has no elements.
contains(Object o)
Returns true if the collection contains the specified element.
containsAll(Collection<?> c)
Returns true if the collection contains all elements of the specified collection.
iterator()
Returns an iterator over the elements.
toArray()
Returns an array containing all elements of the collection.