Java Collections Framework Overview

Loading

The Java Collections Framework (JCF) is a unified architecture for representing and manipulating collections of objects. It provides a set of interfaces, implementations, and algorithms to handle groups of objects efficiently. The framework is part of the java.util package and is one of the most widely used features of Java.


1. Key Components of the Java Collections Framework

The JCF consists of:

  1. Interfaces: Define the contract for collections (e.g., List, Set, Map).
  2. Implementations: Concrete classes that implement the interfaces (e.g., ArrayList, HashSet, HashMap).
  3. Algorithms: Methods to perform operations on collections (e.g., sorting, searching).

2. Core Interfaces

The JCF is built around a set of core interfaces:

a. Collection Interface

  • The root interface for all collections (except Map).
  • Defines basic methods like add(), remove(), size(), and iterator().
  • Sub-interfaces:
  • List: Ordered collection that allows duplicates.
  • Set: Unordered collection that does not allow duplicates.
  • Queue: A collection designed for holding elements prior to processing.

b. Map Interface

  • Represents a key-value pair collection.
  • Does not extend the Collection interface.
  • Examples: HashMap, TreeMap.

3. Common Collection Interfaces and Their Implementations

InterfaceDescriptionImplementations
ListOrdered collection, allows duplicatesArrayList, LinkedList, Vector
SetUnordered collection, no duplicatesHashSet, LinkedHashSet, TreeSet
QueueHolds elements for processingPriorityQueue, LinkedList (as Queue)
MapKey-value pairsHashMap, LinkedHashMap, TreeMap

4. Common Collection Classes

a. List Implementations

  • ArrayList:
  • Resizable array implementation.
  • Fast random access.
  • Not synchronized.
  List<String> list = new ArrayList<>();
  list.add("Apple");
  list.add("Banana");
  System.out.println(list); // Output: [Apple, Banana]
  • LinkedList:
  • Doubly-linked list implementation.
  • Efficient for frequent insertions/deletions.
  List<String> list = new LinkedList<>();
  list.add("Apple");
  list.add("Banana");
  System.out.println(list); // Output: [Apple, Banana]
  • Vector:
  • Synchronized version of ArrayList.
  • Thread-safe but slower.
  List<String> list = new Vector<>();
  list.add("Apple");
  list.add("Banana");
  System.out.println(list); // Output: [Apple, Banana]

b. Set Implementations

  • HashSet:
  • Stores elements in a hash table.
  • No guarantees on order.
  Set<String> set = new HashSet<>();
  set.add("Apple");
  set.add("Banana");
  System.out.println(set); // Output: [Apple, Banana] (order may vary)
  • LinkedHashSet:
  • Maintains insertion order.
  Set<String> set = new LinkedHashSet<>();
  set.add("Apple");
  set.add("Banana");
  System.out.println(set); // Output: [Apple, Banana]
  • TreeSet:
  • Stores elements in a sorted order (natural ordering or custom comparator).
  Set<String> set = new TreeSet<>();
  set.add("Banana");
  set.add("Apple");
  System.out.println(set); // Output: [Apple, Banana] (sorted)

c. Queue Implementations

  • PriorityQueue:
  • Orders elements based on priority (natural ordering or custom comparator).
  Queue<String> queue = new PriorityQueue<>();
  queue.add("Apple");
  queue.add("Banana");
  System.out.println(queue.poll()); // Output: Apple
  • LinkedList (as Queue):
  • Can be used as a FIFO (First-In-First-Out) queue.
  Queue<String> queue = new LinkedList<>();
  queue.add("Apple");
  queue.add("Banana");
  System.out.println(queue.poll()); // Output: Apple

d. Map Implementations

  • HashMap:
  • Stores key-value pairs in a hash table.
  • No guarantees on order.
  Map<String, Integer> map = new HashMap<>();
  map.put("Apple", 1);
  map.put("Banana", 2);
  System.out.println(map); // Output: {Apple=1, Banana=2}
  • LinkedHashMap:
  • Maintains insertion order.
  Map<String, Integer> map = new LinkedHashMap<>();
  map.put("Apple", 1);
  map.put("Banana", 2);
  System.out.println(map); // Output: {Apple=1, Banana=2}
  • TreeMap:
  • Stores key-value pairs in sorted order (natural ordering or custom comparator).
  Map<String, Integer> map = new TreeMap<>();
  map.put("Banana", 2);
  map.put("Apple", 1);
  System.out.println(map); // Output: {Apple=1, Banana=2} (sorted)

5. Common Algorithms

The Collections class provides utility methods for common operations:

  • Sorting:
  List<String> list = new ArrayList<>();
  list.add("Banana");
  list.add("Apple");
  Collections.sort(list);
  System.out.println(list); // Output: [Apple, Banana]
  • Searching:
  int index = Collections.binarySearch(list, "Apple");
  System.out.println("Index of Apple: " + index); // Output: 0
  • Shuffling:
  Collections.shuffle(list);
  System.out.println(list); // Output: Random order

6. Key Points to Remember

  • Use List when you need ordered elements with duplicates.
  • Use Set when you need unique elements.
  • Use Queue when you need FIFO or priority-based processing.
  • Use Map when you need key-value pairs.
  • Prefer ArrayList for frequent access and LinkedList for frequent modifications.
  • Use HashSet for fast lookups and TreeSet for sorted elements.
  • Use HashMap for fast key-value lookups and TreeMap for sorted key-value pairs.

7. Example: Using Collections Framework

import java.util.*;

public class CollectionsExample {
    public static void main(String[] args) {
        // List
        List<String> list = new ArrayList<>();
        list.add("Apple");
        list.add("Banana");
        System.out.println("List: " + list);

        // Set
        Set<String> set = new HashSet<>(list);
        System.out.println("Set: " + set);

        // Map
        Map<String, Integer> map = new HashMap<>();
        map.put("Apple", 1);
        map.put("Banana", 2);
        System.out.println("Map: " + map);

        // Queue
        Queue<String> queue = new LinkedList<>();
        queue.add("Apple");
        queue.add("Banana");
        System.out.println("Queue: " + queue.poll());
    }
}

By mastering the Java Collections Framework, you can efficiently manage and manipulate groups of objects in your applications!

Leave a Reply

Your email address will not be published. Required fields are marked *