Is lesson me hum cover karenge:
- Collection framework kya hota hai
- Collection hierarchy
- List, Set, Map kya hote hain
- Important classes (ArrayList, HashSet, HashMap)
- Real examples aur use cases
Collection framework ka matlab:
data ko store aur manage karne ke liye ready-made classes aur interfaces
Java me:
java.util package me available hota hai
Iterable
↓
Collection
├── List
├── Set
└── Queue
Map (separate interface)
| Term | Meaning |
|---|---|
| Collection | Interface |
| Collections | Utility class |
Example:
Collections.sort(list);List ka use hota hai:
ordered collection + duplicate allowed
- ArrayList
- LinkedList
- Vector
import java.util.ArrayList;
class Test {
public static void main(String[] args){
ArrayList<Integer> list = new ArrayList<>();
list.add(10);
list.add(20);
list.add(10); // duplicate allowed
for(int n : list){
System.out.println(n);
}
}
}import java.util.LinkedList;
class Test {
public static void main(String[] args){
LinkedList<String> list = new LinkedList<>();
list.add("Java");
list.add("Python");
System.out.println(list);
}
}Set ka use hota hai:
unique elements store karne ke liye
✔ duplicates allowed nahi
- HashSet
- LinkedHashSet
- TreeSet
import java.util.HashSet;
class Test {
public static void main(String[] args){
HashSet<Integer> set = new HashSet<>();
set.add(10);
set.add(20);
set.add(10); // duplicate ignore
System.out.println(set);
}
}Map ka use hota hai:
key-value pair store karne ke liye
✔ key unique hoti hai
import java.util.HashMap;
class Test {
public static void main(String[] args){
HashMap<String, Integer> map = new HashMap<>();
map.put("Sujit", 90);
map.put("Rahul", 80);
System.out.println(map.get("Sujit"));
}
}| Feature | List | Set | Map |
|---|---|---|---|
| Order | ✔ Yes | ❌ No | ❌ No |
| Duplicate | ✔ Yes | ❌ No | Key ❌ |
| Index | ✔ Yes | ❌ No | ❌ No |
| Method | Use |
|---|---|
| add() | element add |
| remove() | delete |
| get() | value access |
| contains() | check |
| size() | count |
for(int n : list){
System.out.println(n);
}Iterator<Integer> it = list.iterator();
while(it.hasNext()){
System.out.println(it.next());
}Collections.sort(list);
Collections.reverse(list);
Collections.max(list);ArrayList → dynamic array
HashSet → unique data
HashMap → key-value storage (database-like)
- Collection framework kya hota hai?
- List aur Set me difference kya hai?
- HashMap ka use kya hai?
- ArrayList vs LinkedList difference?
- Iterator kya hota hai?
Is lesson me humne seekha:
✔ Collection framework basics
✔ List, Set, Map
✔ ArrayList, HashSet, HashMap
✔ Iteration methods
✔ Real-life usage
Collection framework Java me dynamic data handling aur efficient storage ke liye use hota hai.
Java Collections Framework (JCF) is a set of classes and interfaces that provide efficient ways to store, manage, and manipulate groups of data.
-
Root interface of collection hierarchy
-
Common methods:
- add()
- remove()
- size()
- isEmpty()
- contains()
- ArrayList
- LinkedList
- Vector (legacy)
- Stack (legacy)
- Maintains insertion order
- Allows duplicate elements
- Index-based access
- HashSet
- LinkedHashSet
- TreeSet
- No duplicate elements
- Uses hashing or tree structure
- PriorityQueue
- ArrayDeque
- offer()
- poll()
- peek()
- HashMap
- LinkedHashMap
- TreeMap
- Hashtable (legacy)
- Keys are unique
- Values can be duplicated
- Dynamic array
- Fast access (O(1))
- Slow insertion/deletion (O(n))
- Doubly linked list
- Fast insertion/deletion
- Slow access
- Based on hashing
- Average O(1) operations
- Sorted structure
- Based on Red-Black Tree
- O(log n) operations
- Based on Heap (Min Heap by default)
- Elements are ordered based on priority
- for loop
- enhanced for loop
- Iterator
- ListIterator
- forEach()
- Used when class defines its own sorting
- Used when we need custom sorting logic
- sort()
- reverse()
- shuffle()
- frequency()
- max()
- min()
| Data Structure | Access | Insert | Delete |
|---|---|---|---|
| ArrayList | O(1) | O(n) | O(n) |
| LinkedList | O(n) | O(1) | O(1) |
| HashMap | O(1) | O(1) | O(1) |
| TreeMap | O(log n) | O(log n) | O(log n) |
| PriorityQueue | O(1)* | O(log n) | O(log n) |
(*peek operation)
- Hashing
- hashCode() and equals()
- Collision handling
- Load factor
- Fail-fast vs Fail-safe
- Vector
- Hashtable
- Collections.synchronizedList()
- ConcurrentHashMap
- Stream API
- Lambda Expressions
- Functional Interfaces
- Parallel Streams
- Ranking system → PriorityQueue
- Unique elements → Set
- Fast lookup → HashMap
- Sorted data → TreeSet
- Not overriding equals() and hashCode()
- Choosing wrong data structure
- Modifying collection during iteration
| Situation | Use |
|---|---|
| Fast search | HashMap |
| Unique data | HashSet |
| Sorted data | TreeSet |
| Frequent insert/delete | LinkedList |
| Priority-based | PriorityQueue |
Java Collections Framework helps in efficient data handling using different data structures and algorithms.
This README is designed for learning, practice, and interview preparation of Java Collections Framework.