Java Collection Framework Overview

Jan 31, 2025

Lecture Notes: Collection Framework in Java

Overview

  • Collection vs Collection Framework
    • Collection: Represents a group of objects as a single entity.
    • Collection Framework: Includes multiple interfaces and classes.

Key Concepts

  • Collection Interface
    • Root interface for all collection-related classes and interfaces.
    • Provides common methods (e.g., adding, removing, checking objects) used across collections.

Main Interfaces in Collection Framework

1. Collection Interface

  • Root interface with common methods for all collections.
  • Methods are used to represent and manipulate group of objects.

2. Collections Class

  • Part of java.util package.
  • Provides utility methods for collection objects.
  • Example: Collections.sort() to sort elements in a collection like ArrayList.

Child Interfaces of Collection

List Interface

  • Child of Collection Interface.
  • **Characteristics: **
    • Preserves insertion order.
    • Allows duplicates.
  • **Implementing Classes: **
    • ArrayList
    • LinkedList
    • Vector
    • Stack

Set Interface

  • Child of Collection Interface.
  • **Characteristics: **
    • Does not preserve insertion order.
    • Does not allow duplicates.
  • **Implementing Classes: **
    • HashSet
    • LinkedHashSet

Queue Interface

  • Child of Collection Interface.
  • **Characteristics: **
    • First In First Out (FIFO) behavior.
    • Used for processing objects in a queue.
  • **Implementing Class: **
    • PriorityQueue

Map Interface

  • Separate from Collection Interface.
  • **Characteristics: **
    • Stores elements in key-value pairs.
    • Keys are unique, values can be duplicated.
  • **Implementing Classes: **
    • HashMap
    • LinkedHashMap

Summary

  • List Interface: Use when insertion order is important and duplicates are needed.
  • Set Interface: Use when no duplicates are required and insertion order does not matter.
  • Queue Interface: Use for processing items in FIFO order.
  • Map Interface: Use to store key-value pairs, allowing unique keys.