

Collection Interface:
The root interface in the collection hierarchy. It represents a group of objects known as its elements.
Key Interfaces Of Collection Framework
NOTE
Maps are not the part of the Collection Framework because there is no relation between Maps and Collection and Maps not implement the Collection interaface
Method | Description |
boolean add(E e) | Add the specified element in the collection. |
boolean addAll(Collection<? extends E> c) | Adds all of the elements in the specified collection to this collection. |
void clear() | Removes all of the elements from this collection. |
boolean contains(Object o) | Returns true if this collection contains the specified element. |
boolean containsAll(Collection<?> c) | Returns true if this collection contains all of the elements in the specified collection. |
boolean equals(Object o) | Compares the specified object with this collection for content equality. |
int hashCode() | Returns the hash code value for this collection. |
boolean isEmpty() | Returns true if this collection contains no elements. |
Iterator<E> iterator() | Returns an iterator over the elements in this collection. |
boolean remove(Object o) | Removes a single instance of the specified element from this collection, if it is present. |
boolean removeAll(Collection<?> c) | Removes all of this collection's elements that are also contained in the specified collection. |
boolean retainAll(Collection<?> c) | Retains only the elements in this collection that are contained in the specified collection. (Intersection between two collection) |
int size() | Returns the number of elements in this collection. |
Object[] toArray() | Returns an array containing all of the elements in this collection. |
<T> T[] toArray(T[] a) | Returns an array containing all of the elements in this collection; the runtime type of the returned array is that of the specified array. |
Compares the specified object with this collection for content equality.
import java.util.*;
public class Test
{
public static void main(String[] args)
{
Collection<String> collection1 = new ArrayList<>();
collection1.add("Hello");
Collection<String> collection2 = new ArrayList<>();
collection2.add("Hello");
boolean isEqual = collection1.equals(collection2);
System.out.println(isEqual); // Output: true
}
}
Returns true if this collection contains no elements.
xxxxxxxxxx
import java.util.*;
public class Test
{
public static void main(String[] args)
{
Collection<String> collection = new ArrayList<>();
boolean isEmpty = collection.isEmpty();
System.out.println(isEmpty); // Output: true
}
}
Add the specified element in the collection.
xxxxxxxxxx
import java.util.*;
public class Test
{
public static void main(String[] args)
{
Collection<String> collection = new ArrayList<>();
boolean isAdded = collection.add("Hello");
System.out.println(isAdded); // Output: true
}
}
Removes a single instance of the specified element from this collection, if it is present.
xxxxxxxxxx
import java.util.*;
public class Test
{
public static void main(String[] args)
{
Collection<String> collection = new ArrayList<>();
collection.add("Hello");
boolean isRemoved = collection.remove("Hello");
System.out.println(isRemoved); // Output: true
System.out.println(collection); // Output: []
}
}
Removes all of this collection's elements that are also contained in the specified collection.
xxxxxxxxxx
import java.util.*;
public class Test
{
public static void main(String[] args)
{
Collection<String> collection = new ArrayList<>();
collection.add("Hello");
collection.add("World");
Collection<String> anotherCollection = new ArrayList<>();
anotherCollection.add("Hello");
boolean isAllRemoved = collection.removeAll(anotherCollection);
System.out.println(isAllRemoved); // Output: true
System.out.println(collection); // Output: [World]
}
}
Adds all of the elements in the specified collection to this collection.
xxxxxxxxxx
import java.util.*;
public class Test
{
public static void main(String[] args)
{
Collection<String> collection = new ArrayList<>();
collection.add("Hello");
Collection<String> anotherCollection = new ArrayList<>();
anotherCollection.add("World");
boolean isAllAdded = collection.addAll(anotherCollection);
System.out.println(isAllAdded); // Output: true
System.out.println(collection); // Output: [Hello, World]
}
}
Removes all of the elements from this collection.
xxxxxxxxxx
import java.util.*;
public class Test
{
public static void main(String[] args)
{
Collection<String> collection = new ArrayList<>();
collection.add("Hello");
collection.clear();
System.out.println(collection); // Output: []
}
}
Returns true
if this collection contains the specified element.
xxxxxxxxxx
import java.util.*;
public class Test
{
public static void main(String[] args)
{
Collection<String> collection = new ArrayList<>();
collection.add("Hello");
collection.add("World");
boolean contains = collection.contains("Hello");
System.out.println(contains); // Output: true
}
}
Returns true if this collection contains all of the elements in the specified collection.
xxxxxxxxxx
import java.util.*;
public class Test
{
public static void main(String[] args)
{
Collection<String> collection = new ArrayList<>();
collection.add("Hello");
collection.add("World");
Collection<String> anotherCollection = new ArrayList<>();
anotherCollection.add("Hello");
anotherCollection.add("World2");
boolean containsAll = collection.containsAll(anotherCollection);
System.out.println(containsAll); // Output: false
}
}