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

Java Collections परिचय

Collections Framework चीज़ों के groups के लिए ready-made containers का एक set है, जैसे lists, sets और maps। यह अलग-अलग storage needs के लिए अलग boxes के एक store जैसा है।

What is the Collections Framework?

Collections Framework Java का standardized set of interfaces and classes है — List, Set, Map, और उनके implementations — objects के groups store और manipulate करने के लिए, अपनी खुद की data structures hand-roll करने की ज़रूरत को replace करते हुए।

उदाहरण: What is the Collections Framework?

java
import java.util.List;
import java.util.ArrayList;
public class Main {
	public static void main(String[] args) {
		List<String> names = new ArrayList<>(); // standardized interface + implementation
		names.add("Alice");
		System.out.println(names);
	}
}

Core Interfaces

List, Set, और Queue तीन core collection interfaces हैं, हर एक ordering और duplicates के आस-पास अलग behavior define करते हुए; Map उनके साथ बैठता है, elements के flat sequence के बजाय key-value pairs store करते हुए।

उदाहरण: Core Interfaces

java
// Import java.util.List so it can be used by its short name
import java.util.List;
// Import java.util.Set so it can be used by its short name
import java.util.Set;
import java.util.Map;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.HashMap;
// 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 list
		List<String> list = new ArrayList<>();
		// Create a new HashSet object and store it in set
		Set<String> set = new HashSet<>();
		Map<String, Integer> map = new HashMap<>();
		// Print a line to the console
		System.out.println(list.size() + " " + set.size() + " " + map.size());
	}
}

Primitives vs Objects

Collections सिर्फ objects store करते हैं, primitives नहीं, इसलिए एक int को एक List<Integer> में जाने से पहले एक Integer में autoboxed होना चाहिए — millions numeric values store करते समय इसके असली performance implications हैं।

उदाहरण: Primitives vs Objects

java
import java.util.List;
import java.util.ArrayList;
public class Main {
	public static void main(String[] args) {
		List<Integer> numbers = new ArrayList<>(); // int autoboxed into Integer
		numbers.add(5);
		int value = numbers.get(0);
		System.out.println(value);
	}
}

The Collections Utility Class

Collections utility class static helper methods प्रदान करता है — sort(), reverse(), max(), unmodifiableList() — जो किसी भी collection पर operate करते हैं बिना आपको खुद common algorithms implement किए।

उदाहरण: The Collections Utility Class

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) {
		// Create a new ArrayList object and store it in numbers
		List<Integer> numbers = new ArrayList<>(List.of(3, 1, 2));
		Collections.sort(numbers);
		// Print a line to the console
		System.out.println(numbers);
	}
}

Advantages of Collections

Concrete classes के बजाय framework के interfaces सीधे उपयोग करना (ArrayList के बजाय List को code करना) आपको बाद में underlying implementation swap करने देता है बिना बाकी code को छुए।

उदाहरण: Advantages of Collections

java
import java.util.List;
import java.util.ArrayList;
public class Main {
	static void printAll(List<String> items) { // coded to the interface, not ArrayList
		for (String item : items) {
			System.out.println(item);
		}
	}
	public static void main(String[] args) {
		printAll(new ArrayList<>(List.of("a", "b")));
	}
}
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. एक collection में int जैसे primitives store करने की कोशिश करना, जब generics को Integer जैसे wrapper types चाहिए।
  2. <String> के बिना List जैसे raw types उपयोग करना, इसलिए type errors runtime पर दिखते हैं।
  3. इसके ऊपर loop करते समय एक collection modify करना, जो ConcurrentModificationException throw करता है।

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.