Introduction
The Collection
interface is the root interface in the Java Collections Framework. It provides the basic methods that are available for all collection interfaces such as List
, Set
, and Queue
. The Collection
interface is a part of the java.util
package and represents a group of objects, known as its elements.
Table of Contents
- What is the Collection Interface?
- Key Points About Collection Interface
- Common Methods of Collection Interface
- Implementations of Collection Interface
- Usage Examples
- Conclusion
1. What is the Collection Interface?
The Collection
interface is the foundation of the Java Collections Framework. It represents a group of objects, known as elements. This interface is the root of the collection hierarchy and provides a common structure for all collections, allowing them to be manipulated independently of the details of their representation.
2. Key Points About Collection Interface
- Root Interface: It is the root interface for the entire Collections Framework.
- Generic: The
Collection
interface uses generics, meaning you can specify the type of elements it contains. - Basic Operations: Provides methods for adding, removing, and querying elements in the collection.
- Subinterfaces: Includes
List
,Set
, andQueue
as subinterfaces, each with its own characteristics and behaviors.
3. Common() Methods of Collection Interface
Here are some of the common methods provided by the Collection
interface:
boolean add(E e)
: Adds the specified element to the collection.boolean remove(Object o)
: Removes a single instance of the specified element from the collection, if it is present.boolean contains(Object o)
: Returnstrue
if the collection contains the specified element.int size()
: Returns the number of elements in the collection.boolean isEmpty()
: Returnstrue
if the collection contains no elements.Iterator<E> iterator()
: Returns an iterator over the elements 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 the collection.boolean containsAll(Collection<?> c)
: Returnstrue
if the collection contains all of the elements in the specified collection.boolean removeAll(Collection<?> c)
: Removes from the collection all of its elements that are also contained in the specified collection.boolean retainAll(Collection<?> c)
: Retains only the elements in the collection that are contained in the specified collection.Object[] toArray()
: Returns an array containing all of the elements in the collection.<T> T[] toArray(T[] a)
: Returns an array containing all of the elements in the collection, with the runtime type of the specified array.
4. Collection Interface Hierarchy
Here are the Collection
interface is extended interfaces and implemented classes in Java:
- List interface: An ordered collection (also known as a sequence). Implementations include
ArrayList
,LinkedList
,Vector
. - Set interface: A collection that does not allow duplicate elements. Implementations include
HashSet
,LinkedHashSet
,TreeSet
. - Queue interface: A collection used to hold multiple elements prior to processing. Implementations include
PriorityQueue
,LinkedList
.
5. Usage Examples
Example 1: Using an ArrayList
An ArrayList
is a resizable array implementation of the List
interface. It allows duplicate elements and maintains the insertion order.
import java.util.ArrayList;
import java.util.Collection;
public class ArrayListExample {
public static void main(String[] args) {
Collection<String> fruits = new ArrayList<>();
fruits.add("Apple");
fruits.add("Banana");
fruits.add("Cherry");
System.out.println("Fruits: " + fruits);
System.out.println("Contains 'Apple': " + fruits.contains("Apple"));
System.out.println("Size: " + fruits.size());
fruits.remove("Banana");
System.out.println("After removal: " + fruits);
}
}
Output:
Fruits: [Apple, Banana, Cherry]
Contains 'Apple': true
Size: 3
After removal: [Apple, Cherry]
Example 2: Using a HashSet
A HashSet
is a hash table implementation of the Set
interface. It does not allow duplicate elements and does not maintain any specific order.
import java.util.HashSet;
import java.util.Collection;
public class HashSetExample {
public static void main(String[] args) {
Collection<String> fruits = new HashSet<>();
fruits.add("Apple");
fruits.add("Banana");
fruits.add("Cherry");
fruits.add("Apple"); // Duplicate element
System.out.println("Fruits: " + fruits);
System.out.println("Contains 'Banana': " + fruits.contains("Banana"));
System.out.println("Size: " + fruits.size());
fruits.remove("Cherry");
System.out.println("After removal: " + fruits);
}
}
Output:
Fruits: [Banana, Apple, Cherry]
Contains 'Banana': true
Size: 3
After removal: [Banana, Apple]
Example 3: Using a PriorityQueue
A PriorityQueue
is a priority heap implementation of the Queue
interface. It orders elements according to their natural ordering or by a comparator provided at queue construction time.
import java.util.PriorityQueue;
import java.util.Collection;
public class PriorityQueueExample {
public static void main(String[] args) {
Collection<Integer> numbers = new PriorityQueue<>();
numbers.add(30);
numbers.add(10);
numbers.add(20);
System.out.println("PriorityQueue: " + numbers);
System.out.println("Contains 20: " + numbers.contains(20));
System.out.println("Size: " + numbers.size());
numbers.remove(10);
System.out.println("After removal: " + numbers);
}
}
Output:
PriorityQueue: [10, 30, 20]
Contains 20: true
Size: 3
After removal: [20, 30]
6. Conclusion
The Collection
interface is the cornerstone of the Java Collections Framework, providing a unified way to work with groups of objects. It defines several essential methods that all collections must implement, ensuring a consistent API across different types of collections.
By understanding the Collection
interface and its methods, you can effectively utilize various collection types in your Java applications to store, manipulate, and retrieve data efficiently. Whether you need an ordered list, a unique set, or a priority-based queue, the Collection
interface and its implementations provide the tools you need to manage your data.