Java Collection Framework – Complete Guide with Examples and FAQs
Java Collection Framework – Complete Guide with Examples and FAQs
🔹 Introduction
Java Collection Framework एक powerful architecture है जो data structures और algorithms को ready-made रूप में provide करता है। इसकी मदद से हम objects के group को आसानी से store, manipulate और process कर सकते हैं।
Arrays की कुछ limitations होती हैं:
-
Fixed size
-
केवल same type data
-
Insertion/Deletion में कठिनाई
👉 इन्हीं problems को solve करने के लिए Collection Framework use होता है।
🔹 Collection Framework Hierarchy
🔹 Main Interfaces and Implementations
1. List Interface
-
Duplicates allowed ✅
-
Insertion order maintained ✅
Implementations: ArrayList, LinkedList, Vector, Stack
Example: ArrayList
Output:
2. Set Interface
-
Duplicates not allowed ❌
-
Order maintained नहीं होता (except LinkedHashSet, TreeSet)
Implementations: HashSet, LinkedHashSet, TreeSet
Example: HashSet
Output:
3. Queue Interface
-
FIFO (First In First Out) principle पर काम करता है
Implementations: PriorityQueue, ArrayDeque
Example: PriorityQueue
Output:
4. Map Interface
-
Data को Key-Value pairs में store करता है
-
Keys unique होते हैं, Values duplicate हो सकती हैं
Implementations: HashMap, LinkedHashMap, TreeMap, Hashtable
Example: HashMap
Output:
🔹 Common Methods in Collection
| Method | Description |
|---|---|
add(E e) | element जोड़ता है |
remove(Object o) | element हटाता है |
clear() | सारे elements हटाता है |
size() | total elements count देता है |
isEmpty() | empty check करता है |
contains(Object o) | element मौजूद है या नहीं check करता है |
iterator() | iterate करने के लिए iterator देता है |
🔹 Comparison Table
| Feature | List | Set | Queue | Map |
|---|---|---|---|---|
| Duplicates | Yes | No | Yes | Keys No, Values Yes |
| Order | Maintained | Not guaranteed (TreeSet = sorted) | FIFO | Depends (LinkedHashMap = insertion, TreeMap = sorted) |
| Null Allowed | Yes | Single null | Yes | Key – 1 null, Values – multiple nulls |
🔹 Advantages of Collection Framework
✅ Dynamic size
✅ Easy to use methods
✅ Better performance in searching/sorting
✅ Different types of data structures
✅ Improves readability and maintainability
🔹 Interview FAQs
Q1: ArrayList और LinkedList में क्या difference है?
-
ArrayList: Fast searching (index based), slow insertion/deletion
-
LinkedList: Fast insertion/deletion, slow searching
Q2: HashMap और Hashtable में क्या फर्क है?
-
HashMap: Not synchronized, allows one null key
-
Hashtable: Synchronized, doesn’t allow null key
Q3: Set duplicates क्यों allow नहीं करता?
क्योंकि Set mathematical "set theory" पर based है, जिसमें हर element unique होता है।
Q4: TreeMap और HashMap में कौन fast है?
-
HashMap: Fast (O(1)) searching और insertion
-
TreeMap: Sorted keys maintain करता है लेकिन slower (O(log n))
Q5: Vector और ArrayList में क्या फर्क है?
-
Vector: Synchronized (thread-safe), old class
-
ArrayList: Not synchronized, faster performance
🎯 Conclusion
Java Collection Framework हमें ready-made data structures provide करता है।
-
जब आपको ordered और duplicate चाहिए → List
-
जब आपको unique values चाहिए → Set
-
जब आपको FIFO/LIFO चाहिए → Queue
-
जब आपको key-value चाहिए → Map
👉 सही Collection चुनना आपके program की performance और readability को boost करता है।
20 Java Collection Framework Examples (Add, Delete, Update, Search)
1. ArrayList – Add Elements
2. ArrayList – Remove Element
3. ArrayList – Update Element (set method)
4. ArrayList – Search Element (contains)
5. ArrayList – Iterate (for-each)
6. ArrayList – Size and Clear
7. LinkedList – Add First/Last
8. LinkedList – Remove First/Last
9. Vector – Add and Iterate
10. Stack – Push and Pop
11. HashSet – Add and Remove
12. HashSet – Search Element
13. LinkedHashSet – Preserve Order
14. TreeSet – Sorted Order
15. PriorityQueue – Add and Poll
16. ArrayDeque – Add First/Last
17. HashMap – Add and Print
18. HashMap – Update and Remove
19. TreeMap – Sorted Keys
20. Hashtable – Add, Search, Remove
🎯 Conclusion
इन 20 examples में आपने Collection Framework के सभी main classes और उनके important methods (add, remove, update, search, iterate, clear, size, etc.) को practically देखा।
ये examples आपके ब्लॉग और interview preparation दोनों के लिए perfect हैं।
Comments
Post a Comment