Java Collection Interface: The Foundation of the Java Collections Framework

Explore the Collection Interface, which serves as the cornerstone of the Java collections framework. This interface defines essential methods applicable to all collection types, including lists, sets, and queues. Learn about basic operations such as adding, removing, and checking for elements, and how the Collection interface is extended by interfaces like List, Set, and Queue to enhance functionality.



Java - Collection Interface

Overview of the Collection Interface

The Collection Interface is the foundation of the Java collections framework. It defines the basic methods that are available to all types of collections, such as lists, sets, and queues. These methods allow basic operations like adding, removing, and checking for the presence of elements in a collection. The Collection interface is extended by several other interfaces, such as List, Set, and Queue.

Collection Interface Methods

Here is a list of the methods provided by the Collection Interface:

Sr.No. Method & Description
1 boolean add(Object obj) - Adds obj to the collection. Returns true if it was added successfully, false if the object is already present or duplicates are not allowed.
2 boolean addAll(Collection c) - Adds all elements from the collection c to the current collection. Returns true if the elements were added successfully.
3 void clear() - Removes all elements from the collection.
4 boolean contains(Object obj) - Returns true if obj is present in the collection, false otherwise.
5 boolean containsAll(Collection c) - Returns true if the collection contains all elements from c.
6 boolean equals(Object obj) - Compares the collection with obj for equality.
7 int hashCode() - Returns the hash code of the collection.
8 boolean isEmpty() - Returns true if the collection is empty, false otherwise.
9 Iterator iterator() - Returns an iterator over the elements in the collection.
10 boolean remove(Object obj) - Removes a single instance of obj from the collection. Returns true if the element was successfully removed.
11 boolean removeAll(Collection c) - Removes all elements of c from the current collection. Returns true if any elements were removed.
12 boolean retainAll(Collection c) - Retains only the elements that are also present in c. Removes the rest.
13 int size() - Returns the number of elements in the collection.
14 Object[] toArray() - Returns an array containing all elements in the collection.
15 Object[] toArray(Object[] array) - Returns an array containing elements of the collection that match the type of the provided array.

Example of Collection Interface in Java

The following example demonstrates the usage of various collection classes and their methods:

import java.util.*;

public class CollectionsDemo {
public static void main(String[] args) {
    // ArrayList example
    List<String> a1 = new ArrayList<>();
    a1.add("Zara");
    a1.add("Mahnaz");
    a1.add("Ayan");
    System.out.println("ArrayList Elements: " + a1);

    // LinkedList example
    List<String> l1 = new LinkedList<>();
    l1.add("Zara");
    l1.add("Mahnaz");
    l1.add("Ayan");
    System.out.println("LinkedList Elements: " + l1);

    // HashSet example
    Set<String> s1 = new HashSet<>();
    s1.add("Zara");
    s1.add("Mahnaz");
    s1.add("Ayan");
    System.out.println("Set Elements: " + s1);

    // HashMap example
    Map<String, String> m1 = new HashMap<>();
    m1.put("Zara", "8");
    m1.put("Mahnaz", "31");
    m1.put("Ayan", "12");
    m1.put("Daisy", "14");
    System.out.println("Map Elements: " + m1);
}
}

Output

ArrayList Elements: [Zara, Mahnaz, Ayan]
LinkedList Elements: [Zara, Mahnaz, Ayan]
Set Elements: [Ayan, Zara, Mahnaz]
Map Elements: {Daisy=14, Ayan=12, Zara=8, Mahnaz=31}