🧺
Collections

Collections: List, Set, Map

Storing Groups of Data
💡 ArrayList एक train है जिसके बोगी order में हैं (index से access)। HashSet एक sticker album है जिसमें कोई sticker repeat नहीं होता। HashMap एक locker room है जहाँ हर locker का अपना नाम-tag (key) होता है।

ArrayList ordered list है, duplicates allowed, index से access होता है।

HashSet सिर्फ़ unique values रखता है, order guarantee नहीं करता। HashMap key-value pairs store करता है — key से तुरंत value ढूँढ सकते हो, जैसे नाम से locker।

List<String> names = new ArrayList<>();
Set<Integer> ids = new HashSet<>();
Map<String, Integer> ages = new HashMap<>();
ages.put("Aarav", 10);
🧺
ArrayList एक train है जिसके बोगी order में हैं (index से access)। HashSet एक sticker album है जिसमें कोई sticker repeat नहीं होता। HashMap एक locker room है जहाँ हर locker का अपना नाम-tag (key) होता है।
1 / 5
⚡ झट से Recap
  • List = ordered, duplicates OK
  • Set = सिर्फ़ unique values
  • Map = key-value pairs
इस page में (5 subtopics)

सबसे ऊपर "Collection" interface है, जिसे List, Set, और Queue extend करते हैं। Map इस hierarchy का हिस्सा नहीं है (क्योंकि वो key-value pairs store करता है, single elements नहीं) — वो अपना अलग interface (Map<K,V>) है जो Collection extend नहीं करता।

हर type एक अलग problem solve करता है: List order + duplicates allow करता है (जैसे एक shopping list — order matter करता है और same item दो बार हो सकता है)। Set सिर्फ uniqueness की guarantee देता है (जैसे एक class के roll numbers — कोई repeat नहीं हो सकता)। Queue processing order define करता है (जैसे एक ticket line — पहले आओ, पहले जाओ)। Map key-based fast lookup देता है (जैसे एक phonebook — नाम से number ढूंढो)।

💡Tip: Interview में "Collection vs Collections" पूछा जाता है — Collection (singular) एक interface है (List/Set/Queue का parent), Collections (plural) एक utility class है जिसमें static helper methods हैं (Collections.sort(), Collections.reverse(), आदि)।

ArrayList, List interface की सबसे commonly used implementation है — internally एक resizable array होती है। add() element जोड़ता है (end में by default, या specific index पर), get(index) retrieve करता है (O(1) — बहुत fast, direct array access जैसा), remove() index या value से हटा सकता है, size() बताता है कितने elements हैं।

contains() check करता है value है या नहीं (O(n) — linear search, क्योंकि ArrayList को पता नहीं कहाँ ढूंढे), isEmpty() बताता है खाली है या नहीं, indexOf() value का position ढूंढता है।

List<String> names = new ArrayList<>();
names.add("Aarav");
names.add("Riya");
System.out.println(names.get(0));   // Aarav
System.out.println(names.contains("Riya")); // true
names.remove("Aarav");
⚠️Common Mistake: ArrayList.contains() या indexOf() O(n) समय लेते हैं (पूरी list scan करते हैं) — अगर बार-बार "क्या यह value है" check करना है बड़े dataset पर, तो HashSet ज़्यादा fast होगा (O(1) average)।

HashSet सबसे fast है (add/remove/contains सब average O(1)) लेकिन कोई order guarantee नहीं (hash-based storage, elements "random" order में दिखते हैं जब print करो)। ज़्यादातर समय यही best choice है जब order matter न करे।

LinkedHashSet insertion order maintain करता है (जिस order में add किया, उसी order में iterate होगा) — थोड़ा slower than HashSet (extra linked-list overhead), लेकिन predictable order चाहिए तो यह चुनो।

TreeSet elements को हमेशा sorted (natural या custom Comparator) order में रखता है — internally एक balanced tree (Red-Black Tree) use करता है, इसलिए operations O(log n) समय लेते हैं (HashSet से slower, लेकिन sorted order मिलता है automatically)।

HashSetLinkedHashSetTreeSet
Orderकोई guarantee नहींInsertion orderSorted order
Speed (add/contains)O(1) averageO(1) averageO(log n)
कब use करेंबस uniqueness चाहिएUniqueness + insertion orderUniqueness + sorted

HashMap हर key का hashCode() calculate करता है, और उस hash के आधार पर decide करता है value कहाँ store होगी ("bucket" — internally एक array of buckets होती है)। यही वजह है key से value ढूंढना average O(1) (बहुत fast) होता है — array index जैसा direct jump, linear search नहीं।

अगर दो keys का hash same आ जाए ("collision"), वो एक ही bucket में linked list (या Java 8+ में अगर बहुत ज़्यादा collisions हों, एक balanced tree) की तरह store होती हैं — worst case थोड़ा slow हो सकता है, लेकिन अच्छे hashCode() implementation से यह rare होता है।

HashMap null key allow करता है (सिर्फ एक), और multiple null values। इससे Hashtable (पुरानी, legacy class) अलग है जो कोई null allow नहीं करती।

Order चाहिए + duplicates OK → ArrayList। सिर्फ unique values, order matter नहीं → HashSet। Unique + insertion order चाहिए → LinkedHashSet। Unique + sorted चाहिए → TreeSet। Key से fast lookup चाहिए → HashMap। Key से lookup + sorted keys चाहिए → TreeMap।

ज़रूरतBest Choice
Ordered list, duplicates OKArrayList
बीच में frequent insert/deleteLinkedList
सिर्फ unique valuesHashSet
Unique + insertion orderLinkedHashSet
Unique + sortedTreeSet
Key-value, fast lookupHashMap
Key-value, sorted keysTreeMap
FIFO processingLinkedList (as Queue) / ArrayDeque