Collection Framework
The Collection Framework is a set of ready-made classes and interfaces in Java used to store, organize, and manipulate groups of objects, such as lists, sets, and maps.
1. What is the Collection Framework?
The Collection Framework is a set of ready-made classes and interfaces in Java used to store, organize, and manipulate groups of objects, such as lists, sets, and maps. It gives programmers a standard, well-tested way to handle groups of data instead of building such structures from scratch.
2. Why is it used?
Arrays have a fixed size and limited built-in functionality. The Collection Framework provides flexible, resizable, and feature-rich alternatives — with built-in sorting, searching, and organizing capabilities already handled for you.
3. Real-Life Example
Think of a toolbox with different compartments for screws, nails, and bolts, each designed for a specific kind of item and a specific way of organizing them. The Collection Framework offers different "compartments" (List, Set, Map) suited to different data-handling needs.
4. Syntax
javaimport java.util.*; // commonly used to bring in collection classes
5. Example Program
javaimport java.util.ArrayList; public class CollectionFrameworkDemo { public static void main(String[] args) { ArrayList<String> fruits = new ArrayList<>(); fruits.add("Apple"); fruits.add("Mango"); System.out.println(fruits); } }
Output:
[Apple, Mango]6. Key Points to Remember
- The three main types of collections are List, Set, and Map, each suited for different needs.
- Collections can grow or shrink dynamically, unlike fixed-size arrays.
- The
java.utilpackage contains most of Java's Collection Framework classes.