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

Iterable (interface) | └── Collection (interface) | ├── List (ArrayList, LinkedList, Vector, Stack) ├── Set (HashSet, LinkedHashSet, TreeSet) └── Queue (PriorityQueue, ArrayDeque) Map (HashMap, LinkedHashMap, TreeMap, Hashtable)

🔹 Main Interfaces and Implementations

1. List Interface

  • Duplicates allowed ✅

  • Insertion order maintained ✅

Implementations: ArrayList, LinkedList, Vector, Stack

Example: ArrayList

import java.util.*; public class ListExample { public static void main(String[] args) { List<String> fruits = new ArrayList<>(); fruits.add("Apple"); fruits.add("Banana"); fruits.add("Mango"); fruits.add("Banana"); // duplicate allowed System.out.println("Fruits List: " + fruits); } }

Output:

Fruits List: [Apple, Banana, Mango, Banana]

2. Set Interface

  • Duplicates not allowed ❌

  • Order maintained नहीं होता (except LinkedHashSet, TreeSet)

Implementations: HashSet, LinkedHashSet, TreeSet

Example: HashSet

import java.util.*; public class SetExample { public static void main(String[] args) { Set<String> cities = new HashSet<>(); cities.add("Delhi"); cities.add("Mumbai"); cities.add("Chennai"); cities.add("Delhi"); // duplicate ignored System.out.println("Cities: " + cities); } }

Output:

Cities: [Delhi, Mumbai, Chennai]

3. Queue Interface

  • FIFO (First In First Out) principle पर काम करता है

Implementations: PriorityQueue, ArrayDeque

Example: PriorityQueue

import java.util.*; public class QueueExample { public static void main(String[] args) { Queue<Integer> numbers = new PriorityQueue<>(); numbers.add(40); numbers.add(10); numbers.add(30); System.out.println("Queue: " + numbers); System.out.println("Head: " + numbers.peek()); System.out.println("Removed: " + numbers.poll()); System.out.println("After Removal: " + numbers); } }

Output:

Queue: [10, 40, 30] Head: 10 Removed: 10 After Removal: [30, 40]

4. Map Interface

  • Data को Key-Value pairs में store करता है

  • Keys unique होते हैं, Values duplicate हो सकती हैं

Implementations: HashMap, LinkedHashMap, TreeMap, Hashtable

Example: HashMap

import java.util.*; public class MapExample { public static void main(String[] args) { Map<Integer, String> students = new HashMap<>(); students.put(1, "Rehan"); students.put(2, "Khan"); students.put(3, "Java"); students.put(2, "Developer"); // key duplicate → overwrite System.out.println("Students: " + students); } }

Output:

Students: {1=Rehan, 2=Developer, 3=Java}

🔹 Common Methods in Collection

MethodDescription
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

FeatureListSetQueueMap
DuplicatesYesNoYesKeys No, Values Yes
OrderMaintainedNot guaranteed (TreeSet = sorted)FIFODepends (LinkedHashMap = insertion, TreeMap = sorted)
Null AllowedYesSingle nullYesKey – 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

import java.util.*; public class Example1 { public static void main(String[] args) { ArrayList<String> list = new ArrayList<>(); list.add("Java"); list.add("Python"); list.add("C++"); System.out.println(list); } }

2. ArrayList – Remove Element

import java.util.*; public class Example2 { public static void main(String[] args) { ArrayList<String> list = new ArrayList<>(Arrays.asList("Java","Python","C++")); list.remove("Python"); System.out.println(list); } }

3. ArrayList – Update Element (set method)

import java.util.*; public class Example3 { public static void main(String[] args) { ArrayList<String> list = new ArrayList<>(Arrays.asList("Java","Python","C++")); list.set(1, "Kotlin"); // update index 1 System.out.println(list); } }

4. ArrayList – Search Element (contains)

import java.util.*; public class Example4 { public static void main(String[] args) { ArrayList<String> list = new ArrayList<>(Arrays.asList("Java","Python","C++")); System.out.println("Contains Python? " + list.contains("Python")); } }

5. ArrayList – Iterate (for-each)

import java.util.*; public class Example5 { public static void main(String[] args) { ArrayList<String> list = new ArrayList<>(Arrays.asList("Java","Python","C++")); for(String lang : list){ System.out.println(lang); } } }

6. ArrayList – Size and Clear

import java.util.*; public class Example6 { public static void main(String[] args) { ArrayList<String> list = new ArrayList<>(Arrays.asList("Java","Python","C++")); System.out.println("Size: " + list.size()); list.clear(); System.out.println("After clear: " + list); } }

7. LinkedList – Add First/Last

import java.util.*; public class Example7 { public static void main(String[] args) { LinkedList<String> ll = new LinkedList<>(); ll.addFirst("First"); ll.addLast("Last"); System.out.println(ll); } }

8. LinkedList – Remove First/Last

import java.util.*; public class Example8 { public static void main(String[] args) { LinkedList<String> ll = new LinkedList<>(Arrays.asList("One","Two","Three")); ll.removeFirst(); ll.removeLast(); System.out.println(ll); } }

9. Vector – Add and Iterate

import java.util.*; public class Example9 { public static void main(String[] args) { Vector<Integer> v = new Vector<>(); v.add(10); v.add(20); v.add(30); for(Integer i : v){ System.out.println(i); } } }

10. Stack – Push and Pop

import java.util.*; public class Example10 { public static void main(String[] args) { Stack<String> stack = new Stack<>(); stack.push("A"); stack.push("B"); stack.push("C"); System.out.println("Pop: " + stack.pop()); System.out.println("Stack: " + stack); } }

11. HashSet – Add and Remove

import java.util.*; public class Example11 { public static void main(String[] args) { HashSet<String> set = new HashSet<>(); set.add("Apple"); set.add("Banana"); set.remove("Banana"); System.out.println(set); } }

12. HashSet – Search Element

import java.util.*; public class Example12 { public static void main(String[] args) { HashSet<String> set = new HashSet<>(Arrays.asList("Apple","Banana","Mango")); System.out.println("Contains Mango? " + set.contains("Mango")); } }

13. LinkedHashSet – Preserve Order

import java.util.*; public class Example13 { public static void main(String[] args) { LinkedHashSet<String> set = new LinkedHashSet<>(); set.add("Delhi"); set.add("Mumbai"); set.add("Chennai"); System.out.println(set); } }

14. TreeSet – Sorted Order

import java.util.*; public class Example14 { public static void main(String[] args) { TreeSet<Integer> ts = new TreeSet<>(); ts.add(30); ts.add(10); ts.add(20); System.out.println(ts); } }

15. PriorityQueue – Add and Poll

import java.util.*; public class Example15 { public static void main(String[] args) { PriorityQueue<Integer> pq = new PriorityQueue<>(); pq.add(50); pq.add(10); pq.add(30); System.out.println("Head: " + pq.peek()); System.out.println("Removed: " + pq.poll()); System.out.println("After poll: " + pq); } }

16. ArrayDeque – Add First/Last

import java.util.*; public class Example16 { public static void main(String[] args) { ArrayDeque<String> dq = new ArrayDeque<>(); dq.addFirst("Front"); dq.addLast("Back"); System.out.println(dq); } }

17. HashMap – Add and Print

import java.util.*; public class Example17 { public static void main(String[] args) { HashMap<Integer,String> map = new HashMap<>(); map.put(1,"A"); map.put(2,"B"); map.put(3,"C"); System.out.println(map); } }

18. HashMap – Update and Remove

import java.util.*; public class Example18 { public static void main(String[] args) { HashMap<Integer,String> map = new HashMap<>(); map.put(1,"A"); map.put(2,"B"); map.put(3,"C"); map.put(2,"Updated B"); // update map.remove(3); // remove key 3 System.out.println(map); } }

19. TreeMap – Sorted Keys

import java.util.*; public class Example19 { public static void main(String[] args) { TreeMap<Integer,String> tm = new TreeMap<>(); tm.put(3,"C"); tm.put(1,"A"); tm.put(2,"B"); System.out.println(tm); } }

20. Hashtable – Add, Search, Remove

import java.util.*; public class Example20 { public static void main(String[] args) { Hashtable<Integer,String> ht = new Hashtable<>(); ht.put(1,"Red"); ht.put(2,"Blue"); ht.put(3,"Green"); System.out.println("Contains Key 2? " + ht.containsKey(2)); ht.remove(3); System.out.println(ht); } }

🎯 Conclusion

इन 20 examples में आपने Collection Framework के सभी main classes और उनके important methods (add, remove, update, search, iterate, clear, size, etc.) को practically देखा।
ये examples आपके ब्लॉग और interview preparation दोनों के लिए perfect हैं।

Comments

Popular posts from this blog

📘 Top 500 Java Interview Questions (With Topics)

Git And GitHub Collaborators and teams

Android Interview Question and Answer