← Back to Core Java Course | Chapter 11: Collections | Lesson 16 of 17

Java की Collections Class

Collections class collections के लिए helpers का एक toolbox है, जैसे sorting, reversing, shuffling, और searching। यह आपको वे steps खुद लिखने से बचाता है।
Syntax
java
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

java
// 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);
	}
}

Reversing and Shuffling

Collections.reverse() एक list का order जगह पर flip करता है, और Collections.shuffle() इसे randomize करता है — दोनों सीधे उस list पर operate करते हैं जो आप pass करते हैं बजाय एक नया return करने के।

उदाहरण: Reversing and Shuffling

java
// 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);
	}
}

Searching Collections

Collections.binarySearch() एक *sorted* list में एक element की index O(log n) time में ढूंढता है — इसे एक unsorted list पर call करना undefined, unreliable results देता है, इसलिए हमेशा पहले sort करें।

उदाहरण: Searching Collections

java
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);
	}
}

Modifying Lists

Collections.max(), min(), frequency(), और swap() जैसे methods आपको इन बहुत आम operations के लिए manual loops लिखे बिना list contents query या rearrange करने देते हैं।

उदाहरण: Modifying Lists

java
// 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);
	}
}

Unmodifiable Collections

Collections.unmodifiableList() (और इसके Set/Map equivalents) एक collection को wrap करता है ताकि कोई भी modification की कोशिश UnsupportedOperationException throw करे, आपको internal data का एक read-only view सुरक्षित रूप से देने देते हुए।

उदाहरण: Unmodifiable Collections

java
// 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());
		}
	}
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. ऐसे objects की एक list पर Collections.sort() call करना जो Comparable नहीं हैं, जो compile नहीं होता या fail होता है।
  2. एक unmodifiable list modify करना, जो UnsupportedOperationException throw करता है।
  3. एक unsorted list पर binarySearch() उपयोग करना, जो गलत results देता है।

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.