`

The Java Collection Framework (JCF) is a set of classes and interfaces in the java.util package that provides data structures and algorithms for storing, retrieving, and manipulating collections of objects.


1. Hierarchy of Java Collection Framework

The Collection Framework has two main root interfaces:

  1. Collection Interface:
    • Represents a group of individual elements.
    • Subinterfaces: List, Set, Queue, Deque.
  2. Map Interface:
    • Represents a collection of key-value pairs.

Java Collection Framework Overview:


2. Key Interfaces and Classes

Collection Interface

The Collection interface is the root for most collection classes. Key operations include adding, removing, and iterating elements.

List Interface

Set Interface

Queue and Deque

Map Interface


3. Key Methods in Collection Framework

Common Methods in Collection Interface:

Method Description
add(E e) Adds an element to the collection.
remove(Object o) Removes a single instance of the specified element.
size() Returns the number of elements in the collection.
clear() Removes all elements.
contains(Object o) Checks if the collection contains the specified element.
isEmpty() Checks if the collection is empty.
iterator() Returns an iterator for traversing the collection.

Additional Methods in List Interface:

Method Description
get(int index) Returns the element at the specified position.
set(int index, E element) Replaces the element at the specified position.
add(int index, E element) Inserts an element at the specified position.
remove(int index) Removes the element at the specified position.

Additional Methods in Map Interface:

Method Description
put(K key, V value) Associates the specified value with the specified key.
get(Object key) Returns the value mapped to the specified key.
remove(Object key) Removes the mapping for the specified key.
containsKey(Object key) Checks if the map contains the specified key.
containsValue(Object value) Checks if the map contains the specified value.

4. Examples

ArrayList Example:

import java.util.ArrayList;

public class ArrayListExample {
    public static void main(String[] args) {
        ArrayList<String> list = new ArrayList<>();
        list.add("Java");
        list.add("Python");
        list.add("C++");
        
        System.out.println("ArrayList: " + list);
        list.remove("Python");
        System.out.println("After removal: " + list);
    }
}

HashSet Example:

import java.util.HashSet;

public class HashSetExample {
    public static void main(String[] args) {
        HashSet<Integer> set = new HashSet<>();
        set.add(1);
        set.add(2);
        set.add(3);
        set.add(1); // Duplicate, will not be added
        
        System.out.println("HashSet: " + set);
    }
}

HashMap Example:

import java.util.HashMap;

public class HashMapExample {
    public static void main(String[] args) {
        HashMap<Integer, String> map = new HashMap<>();
        map.put(1, "Java");
        map.put(2, "Python");
        map.put(3, "C++");
        
        System.out.println("HashMap: " + map);
        System.out.println("Value for key 2: " + map.get(2));
        map.remove(3);
        System.out.println("After removal: " + map);
    }
}

5. Key Advantages of Collection Framework

  1. Reduces Programming Effort: Provides ready-made data structures and algorithms.
  2. Improves Performance: Optimized implementations of data structures.
  3. Flexibility: Different implementations for various requirements (e.g., ordered, sorted, synchronized).
  4. Thread Safety: Classes like Vector and Hashtable are thread-safe.

6. Comparison of Common Classes

Feature ArrayList LinkedList HashSet HashMap
Order Maintains insertion order Maintains insertion order No ordering No ordering
Duplicates Allowed Allowed Not allowed Not allowed (keys)
Null Elements Allowed Allowed Allows one Allows one key
Performance Fast random access Fast insertion/deletion Fast operations Fast operations
Thread Safety Not thread-safe Not thread-safe Not thread-safe Not thread-safe