Java की Collections Class
In this page:
import java.util.Collections;
Collections.sort(list);
Collections.reverse(list);
Collections.max(collection);
Sorting Collections
Collections.sort() natural ordering या एक supplied Comparator के अनुसार एक List को जगह पर reorder करता है, आपको everyday use cases के लिए खुद एक sorting algorithm implement करने से बचाते हुए।
उदाहरण: Sorting Collections
// Import java.util.Collections so it can be used by its short name
import java.util.Collections;
// Import java.util.ArrayList so it can be used by its short name
import java.util.ArrayList;
// Define the class Main
public class Main {
// Program entry point: the JVM starts running here
public static void main(String[] args) {
// Create a new ArrayList object and store it in nums
ArrayList<Integer> nums = new ArrayList<>(java.util.List.of(3, 1, 2));
Collections.sort(nums);
// Print a line to the console
System.out.println(nums);
}
}
Login to try C/C++/Java/PHP code in the editor
Reversing and Shuffling
Collections.reverse() एक list का order जगह पर flip करता है, और Collections.shuffle() इसे randomize करता है — दोनों सीधे उस list पर operate करते हैं जो आप pass करते हैं बजाय एक नया return करने के।
उदाहरण: Reversing and Shuffling
// Import java.util.Collections so it can be used by its short name
import java.util.Collections;
// Import java.util.ArrayList so it can be used by its short name
import java.util.ArrayList;
// Define the class Main
public class Main {
// Program entry point: the JVM starts running here
public static void main(String[] args) {
// Create a new ArrayList object and store it in nums
ArrayList<Integer> nums = new ArrayList<>(java.util.List.of(1, 2, 3));
Collections.reverse(nums);
// Print a line to the console
System.out.println(nums);
}
}
Login to try C/C++/Java/PHP code in the editor
Searching Collections
Collections.binarySearch() एक *sorted* list में एक element की index O(log n) time में ढूंढता है — इसे एक unsorted list पर call करना undefined, unreliable results देता है, इसलिए हमेशा पहले sort करें।
उदाहरण: Searching Collections
import java.util.Collections;
import java.util.ArrayList;
public class Main {
public static void main(String[] args) {
ArrayList<Integer> nums = new ArrayList<>(java.util.List.of(1, 3, 5, 7));
int index = Collections.binarySearch(nums, 5); // list must already be sorted
System.out.println(index);
}
}
Login to try C/C++/Java/PHP code in the editor
Modifying Lists
Collections.max(), min(), frequency(), और swap() जैसे methods आपको इन बहुत आम operations के लिए manual loops लिखे बिना list contents query या rearrange करने देते हैं।
उदाहरण: Modifying Lists
// Import java.util.Collections so it can be used by its short name
import java.util.Collections;
// Import java.util.ArrayList so it can be used by its short name
import java.util.ArrayList;
// Define the class Main
public class Main {
// Program entry point: the JVM starts running here
public static void main(String[] args) {
// Create a new ArrayList object and store it in nums
ArrayList<Integer> nums = new ArrayList<>(java.util.List.of(3, 1, 4, 1));
// Print a line to the console
System.out.println(Collections.max(nums));
// Print a line to the console
System.out.println(Collections.frequency(nums, 1));
Collections.swap(nums, 0, 1);
System.out.println(nums);
}
}
Login to try C/C++/Java/PHP code in the editor
Unmodifiable Collections
Collections.unmodifiableList() (और इसके Set/Map equivalents) एक collection को wrap करता है ताकि कोई भी modification की कोशिश UnsupportedOperationException throw करे, आपको internal data का एक read-only view सुरक्षित रूप से देने देते हुए।
उदाहरण: Unmodifiable Collections
// Import java.util.Collections so it can be used by its short name
import java.util.Collections;
// Import java.util.ArrayList so it can be used by its short name
import java.util.ArrayList;
import java.util.List;
// Define the class Main
public class Main {
// Program entry point: the JVM starts running here
public static void main(String[] args) {
List<Integer> readOnly = Collections.unmodifiableList(new ArrayList<>(java.util.List.of(1, 2)));
// Try code that might throw an exception
try {
readOnly.add(3);
// Handle UnsupportedOperationException if it was thrown
} catch (UnsupportedOperationException e) {
// Print a line to the console
System.out.println("Cannot modify: " + e.getClass().getSimpleName());
}
}
}
Login to try C/C++/Java/PHP code in the editor
- ऐसे objects की एक list पर
Collections.sort()call करना जोComparableनहीं हैं, जो compile नहीं होता या fail होता है। - एक unmodifiable list modify करना, जो
UnsupportedOperationExceptionthrow करता है। - एक unsorted list पर
binarySearch()उपयोग करना, जो गलत results देता है।
Chapter Quiz — Complete all 17 topics to unlock
0/17 topics done
Complete these topics first:
- Java Data Structures
- Java Collections Introduction
- Java List Interface
- Java ArrayList
- Java LinkedList
- Java Map Interface
- Java HashMap
- Java Set Interface
- Java TreeMap
- Java LinkedHashMap
- Java HashSet
- Java TreeSet
- Java LinkedHashSet
- Java Stack & Queue
- Java Iterator
- Java Collections Class
- Java List Sorting