Java ArrayList: In-Depth Guide with Performance Tips
Java ArrayList: In-Depth Guide with Performance Tips
Introduction
अगर आप Java सीख रहे हैं और collections के बारे में समझना चाहते हैं, तो ArrayList एक ऐसा topic है जो हर exam और interview में जरूर पूछा जाता है। यह Java Collection Framework का बहुत ही important हिस्सा है। आज हम इस blog में समझेंगे कि ArrayList क्या है, कैसे काम करता है, इसके methods, performance tips और best practices क्या हैं — वो भी एकदम आसान भाषा में।
What is ArrayList?
ArrayList Java में एक resizable array है यानी इसकी size को run-time पर बढ़ाया या घटाया जा सकता है। यह java.util package का हिस्सा है और List interface को implement करता है। अगर simple शब्दों में कहें तो ArrayList एक dynamic array की तरह काम करता है।
Key Features of ArrayList
- Dynamic resizing — elements बढ़ने पर size अपने आप बढ़ता है।
- Duplicate elements को allow करता है।
- Insertion order maintain करता है।
- Random access possible होता है क्योंकि internally array use होता है।
- Non-synchronized होता है (Thread-safe नहीं)।
Declaration and Initialization
ArrayList को declare और initialize करने के कई तरीके हैं। नीचे कुछ examples दिए गए हैं:
// Basic declaration
ArrayList list = new ArrayList<>();
// With initial capacity
ArrayList numbers = new ArrayList<>(10);
// Using List interface reference
List names = new ArrayList<>();
Internal Working of ArrayList
ArrayList internally एक simple array का use करता है। जब आप कोई नया element add करते हैं और capacity भर जाती है, तो ArrayList अपनी capacity को 50% increase कर देता है और नया array बनाकर पुराने elements को copy करता है। इस process को Dynamic Array Resizing कहा जाता है।
By default, जब आप एक empty ArrayList बनाते हैं और उसमें element add करते हैं, तो उसकी initial capacity 10 होती है।
ArrayList Growth Mechanism
जब ArrayList की capacity पूरी हो जाती है:
- एक नया array create होता है जिसका size = (oldCapacity * 1.5) + 1 होता है।
- पुराने elements को नए array में copy किया जाता है।
Important Methods of ArrayList
ArrayList में कई commonly used methods होते हैं जो exam और interview दोनों में बहुत काम आते हैं:
| Method | Description |
|---|---|
add(E e) |
Element को list में add करता है। |
add(int index, E e) |
Specific index पर element insert करता है। |
get(int index) |
Index के basis पर element return करता है। |
set(int index, E e) |
Existing element को replace करता है। |
remove(int index) |
Specific position का element remove करता है। |
size() |
Total elements count देता है। |
clear() |
सभी elements को remove करता है। |
contains(Object o) |
Check करता है कि element list में है या नहीं। |
Example Code
चलो एक simple example से समझते हैं कि ArrayList practically कैसे काम करता है:
import java.util.*;
public class ArrayListExample {
public static void main(String[] args) {
ArrayList fruits = new ArrayList<>();
fruits.add("Apple");
fruits.add("Banana");
fruits.add("Mango");
fruits.add("Orange");
System.out.println("Fruits List: " + fruits);
fruits.remove("Banana");
System.out.println("After Removing Banana: " + fruits);
fruits.add(1, "Grapes");
System.out.println("After Adding Grapes at Index 1: " + fruits);
System.out.println("Total Fruits: " + fruits.size());
}
}
Performance Analysis of ArrayList
अब बात करते हैं performance की, जो exam और interview दोनों में बहुत पूछी जाती है। ArrayList की performance उसके operations पर depend करती है।
| Operation | Time Complexity | Explanation |
|---|---|---|
get() |
O(1) | Direct index access के कारण बहुत fast। |
add() |
O(1) (amortized) | End में element add करना तेज होता है। |
add(index, element) |
O(n) | Shifting की जरूरत पड़ती है। |
remove() |
O(n) | Remove करने पर shifting होती है। |
contains() |
O(n) | हर element को check करना पड़ता है। |
Memory Efficiency
ArrayList की memory allocation थोड़ी flexible होती है लेकिन frequent resizing avoid करना चाहिए क्योंकि इससे performance पर impact पड़ता है। अगर आपको अंदाज़ा है कि कितने elements add होंगे, तो constructor में initial capacity specify करें।
ArrayList list = new ArrayList<>(1000);
ArrayList vs LinkedList
यह comparison भी बहुत exam specific है। दोनों List interface को implement करते हैं लेकिन internal working अलग है।
| Feature | ArrayList | LinkedList |
|---|---|---|
| Data Structure | Dynamic Array | Doubly Linked List |
| Access Speed | Fast (O(1)) | Slow (O(n)) |
| Insertion/Deletion | Slow (Shifting required) | Fast (No shifting) |
| Memory Usage | Less memory | More memory |
| Use Case | Frequent access | Frequent insertion/deletion |
Performance Tips for ArrayList
ArrayList की performance को optimize करने के लिए कुछ smart techniques use की जा सकती हैं:
- Initial capacity को estimate करें ताकि resizing कम हो।
- Iterator का use करें traversal के लिए, ताकि ConcurrentModificationException ना आए।
- Unnecessary remove() operations avoid करें क्योंकि shifting costly होती है।
- अगर thread-safety चाहिए तो
Collections.synchronizedList()का use करें। - Large data के लिए
ensureCapacity()method use करें।
ArrayList list = new ArrayList<>();
list.ensureCapacity(5000);
Best Practices for Using ArrayList
- अगर data बहुत बड़ा है और बार-बार remove करना है, तो LinkedList बेहतर रहेगा।
- Null elements का use avoid करें।
- अगर read-heavy operations हैं तो ArrayList perfect है।
- ArrayList में type safety के लिए Generics का use हमेशा करें।
Real-Life Example of ArrayList
मान लीजिए आप एक student record system बना रहे हैं जहाँ आपको students के नाम store करने हैं। वहाँ ArrayList perfect रहेगा क्योंकि data sequential है और बार-बार access करना है।
import java.util.*;
class StudentRecords {
public static void main(String[] args) {
ArrayList students = new ArrayList<>();
students.add("Ravi");
students.add("Priya");
students.add("Arjun");
students.add("Meena");
for(String s : students) {
System.out.println("Student Name: " + s);
}
}
}
ArrayList – Exam & Interview Quick Notes
- ArrayList dynamic array की तरह काम करता है।
- Thread-safe नहीं होता।
- Random access possible है।
- Default capacity 10 होती है।
- Duplicate elements allow करता है।
- Insertion order maintain रहता है।
- Implements List interface और extends AbstractList।
- Performance wise best for read-heavy applications।
Summary Notes for Exam
- Primary Keyword: Java ArrayList
- Core Concept: Dynamic array with resizing feature
- Complexity: get() – O(1), remove() – O(n)
- Package: java.util
- Use Case: Data access frequent हो तो ArrayList ideal है।
- Alternate: Frequent deletion के लिए LinkedList।