Java Collection Framework – Complete Beginner Guide
📘 What is the Collection Framework?
The Java Collection Framework is a set of classes and interfaces provided by Java to store, manage, and process groups of objects efficiently.
👉 It is part of the java.util package.
📌 Key Points
- The Collection Framework stores objects only, not primitive data types
- Primitive data types are stored using Autoboxing
Example: int → Integer, double → Double
- Collection size is dynamic, controlled by the JVM
- Provides ready-made methods for searching, sorting, inserting, and deleting data
📘 Real-Life Example
📚 Library System
- A library stores many books
- Each book is an object
- Java collections store these book objects efficiently
📘 Main Interfaces in Collection Framework
Interfaces:
- Collection
- List
- Set
- Queue
- Map
📘 Advantages of Collection Framework
✔ Stores homogeneous and heterogeneous objects
✔ Dynamic size (no fixed size like arrays)
✔ Built-in methods for data processing
✔ Improves performance and code readability
📘 Common Collection Interface Methods
isEmpty(). Checks if collection is empty
size(). Returns number of elements
add(Object o). Adds an element
remove(Object o). Removes an element
contains(Object o). Checks presence
addAll(Collection c). Adds another collection
removeAll(Collection c). Removes all matching
retainAll(Collection c). Keeps common elements
iterator(). Iterates elements
📘 List Interface
Characteristics
- Maintains insertion order
- Allows duplicate elements
- Allows null values
- List-Specific Methods
add(int index, Object o). Insert at index
set(int index, Object o). Replace element
get(int index). Fetch element
📘 ArrayList Class
- Package: java.util
- Resizable array-based structure
- Default capacity: 10
- Growth formula:
- new capacity = old capacity × 1.5
Supports:
✔ Null and duplicate null values
ArrayList<Integer> list = new ArrayList<>();
📘 Vector Class
- Thread-safe (synchronized)
- Default capacity: 10
- Growth formula:
- new capacity = old capacity × 2
Supports:
Vector<String> v = new Vector<>();
📘 LinkedList Class
- Node-based data structure
- Implements List, Queue, Collection
- Better for frequent insert/delete operations
Supports:
LinkedList<String> list = new LinkedList<>();
📘 Queue Interface – PriorityQueue
PriorityQueue Class
- Uses priority-based ordering
- Lowest element has highest priority
- Does not maintain insertion order
- Does not allow null values
Supports:
PriorityQueue<Integer> pq = new PriorityQueue<>();
📘 Set Interface
- HashSet Class
- No insertion order
- No duplicate elements
- Allows one null value
- Default capacity: 16
- Load factor: 0.75
HashSet<String> set = new HashSet<>();
LinkedHashSet Class
- Maintains insertion order
- No duplicates
- Allows one null value
LinkedHashSet<Integer> set = new LinkedHashSet<>();
TreeSet Class
- Stores elements in ascending sorted order
- Does not allow duplicates
- Does not allow null values
TreeSet<Integer> set = new TreeSet<>();
📘 Generics in Collection Framework
Generics provide type safety by ensuring collections store only one type of object.
ArrayList<String> list = new ArrayList<>();
✔ Prevents ClassCastException
✔ Improves compile-time checking
📘 Map Interface
- Stores data as key–value pairs
- Keys are unique
- Values can be duplicate
Common Map Methods
containsKey(k). Check key
containsValue(v) Check value
📘 HashMap Class
- No insertion order
- One null key allowed
- Multiple null values allowed
- Duplicate keys overwrite previous value
HashMap<Integer, String> map = new HashMap<>();
📘 TreeMap Class
- Stores data in sorted order of keys
- Does not allow null keys
- Duplicate keys overwrite values
TreeMap<Integer, String> map = new TreeMap<>();
📘 HashMap vs TreeMap
Null Key Allowed (1) Not Allowed
Performance Faster Slower
📌 Exam Tip (Very Important)
- Collections store objects, not primitive data types.
- Autoboxing converts primitives into wrapper objects automatically.
Comments
Post a Comment