Is lesson me hum seekhenge:
- Collections class kya hoti hai
- Collection vs Collections difference
- Important methods of Collections class
- Sorting, reversing, searching operations
- Real examples
Collections ek utility class hai jo provide karti hai:
static methods for collection operations
Ye class:
java.util package me hoti hai
| Feature | Collection | Collections |
|---|---|---|
| Type | Interface | Class |
| Use | data store karna | operations perform karna |
| Method | Use |
|---|---|
| sort() | sorting |
| reverse() | reverse order |
| max() | maximum value |
| min() | minimum value |
| shuffle() | random order |
| binarySearch() | searching |
| frequency() | count occurrences |
import java.util.*;
class Test {
public static void main(String[] args){
ArrayList<Integer> list = new ArrayList<>();
list.add(30);
list.add(10);
list.add(20);
Collections.sort(list);
System.out.println(list);
}
}Output:
[10, 20, 30]
Collections.reverse(list);System.out.println(Collections.max(list));
System.out.println(Collections.min(list));Collections.shuffle(list);Random order me elements arrange honge.
Collections.sort(list);
int index = Collections.binarySearch(list, 20);
System.out.println(index);Note:
list sorted hona chahiye
int count = Collections.frequency(list, 10);
System.out.println(count);Collections.sort(list, Comparator.reverseOrder());List<Integer> list = Collections.unmodifiableList(new ArrayList<>());Isme modification allowed nahi.
import java.util.*;
class Test {
public static void main(String[] args){
ArrayList<String> names = new ArrayList<>();
names.add("Sujit");
names.add("Rahul");
names.add("Amit");
Collections.sort(names);
System.out.println(names);
}
}✔ Collections class ke sab methods static hote hain
✔ Ye sirf operations perform karti hai
✔ Data store nahi karti
- Collection aur Collections me difference kya hai?
- Collections.sort() kaise kaam karta hai?
- Binary search me sorting kyun zaruri hai?
- shuffle() method ka use kya hai?
Is lesson me humne seekha:
✔ Collections class concept
✔ Important utility methods
✔ Sorting, searching, reversing
✔ Real-life examples
Collections class Java me collection operations ko easy aur efficient banati hai.