← Back to Advanced Java Course | Chapter 3: Functional Programming | Lesson 3 of 6

Java का Stream API

एक stream items की एक conveyor belt है जहां हर step उन्हें filter, बदल, या गिन सकता है, जैसे एक factory line। यह आपको data को एक स्पष्ट chain में process करने देता है।
Syntax
java
collection.stream()
    .filter(item -> condition)
    .map(item -> expression)
    .collect(Collectors.toList());

What is a Stream?

एक Stream elements की एक sequence पर लागू किए गए computational steps की एक pipeline को represent करता है। यह आपको किसी collection के data को declarative, functional style में process करने देता है, एक explicit loop लिखने के बजाय आप चाहते transformation describe करते हुए, और यह underlying data source को कभी modify नहीं करता।

उदाहरण: What is a Stream?

java
import java.util.*;
public class Main {
	public static void main(String[] args) {
		List<Integer> nums = List.of(1, 2, 3, 4, 5);
		long count = nums.stream().count(); // declarative, no explicit loop
		System.out.println(count);
	}
}

Filter and Map

filter() operation सिर्फ वे elements चुनता है जो किसी दी गई condition से मेल खाते हों, बाकी को pipeline से हटाते हुए। map() operation इसके बजाय stream के हर element को एक नई value में बदल देता है, एक-के-लिए-एक, मौजूद elements की संख्या बदले बिना।

उदाहरण: Filter and Map

java
// Import java.util.* so it can be used by its short name
import java.util.*;
// Import java.util.stream.* so it can be used by its short name
import java.util.stream.*;
// Define the class Main
public class Main {
	// Program entry point: the JVM starts running here
	public static void main(String[] args) {
		List<Integer> nums = List.of(1, 2, 3, 4, 5, 6);
		// Create a stream from the collection to process it step by step
		List<Integer> result = nums.stream()
			.filter(n -> n % 2 == 0)
			.map(n -> n * n)
			.collect(Collectors.toList());
		// Print a line to the console
		System.out.println(result);
	}
}

Terminal Operations - Collect

Terminal operations वे हैं जो असल में किसी stream pipeline को execute करने के लिए trigger करते हैं, क्योंकि filter() और map() जैसे intermediate operations lazy हैं और किसी terminal operation के call होने तक कुछ नहीं करते। collect() terminal operation pipeline के अंतिम results को किसी concrete container में वापस इकट्ठा करता है, जैसे List या Set।

उदाहरण: Terminal Operations - Collect

java
import java.util.*;
import java.util.stream.*;
public class Main {
	public static void main(String[] args) {
		List<String> names = List.of("Riya", "Aman", "Zoya");
		List<String> upper = names.stream()
			.map(String::toUpperCase)
			.collect(Collectors.toList()); // triggers the pipeline
		System.out.println(upper);
	}
}

Reduction and Matching

आप reduce() का उपयोग करके किसी stream के हर element को एक single value में combine कर सकते हैं, उदाहरण के लिए numbers की एक list को sum करने या maximum ढूंढने के लिए। आप anyMatch(), allMatch(), या noneMatch() जैसे short-circuiting matchers का उपयोग करके यह भी जांच सकते हैं कि elements किसी condition को satisfy करते हैं या नहीं, जो जवाब तय होते ही scan करना रोक देते हैं।

उदाहरण: Reduction and Matching

java
// Import java.util.* so it can be used by its short name
import java.util.*;
// Define the class Main
public class Main {
	// Program entry point: the JVM starts running here
	public static void main(String[] args) {
		List<Integer> nums = List.of(1, 2, 3, 4, 5);
		// Create a stream from the collection to process it step by step
		int sum = nums.stream().reduce(0, Integer::sum);
		// Create a stream from the collection to process it step by step
		boolean anyEven = nums.stream().anyMatch(n -> n % 2 == 0);
		// Print a line to the console
		System.out.println(sum + " " + anyEven);
	}
}

Parallel Streams

आप stream() के बजाय सिर्फ parallelStream() call करके कई CPU cores में stream के operations को parallel में execute कर सकते हैं। सुनिश्चित करें कि आप जो भी operations इस पर chain करते हैं वे stateless हों और shared mutable state से मुक्त हों, क्योंकि parallel execution अन्यथा debug करने में मुश्किल data races introduce कर सकता है।

उदाहरण: Parallel Streams

java
// Import java.util.* so it can be used by its short name
import java.util.*;
// Define the class Main
public class Main {
	// Program entry point: the JVM starts running here
	public static void main(String[] args) {
		List<Integer> nums = List.of(1, 2, 3, 4, 5);
		int sum = nums.parallelStream()
			.mapToInt(Integer::intValue)
			.sum();
		// Print a line to the console
		System.out.println(sum);
	}
}
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. count() जैसे किसी terminal operation के बाद किसी stream को फिर से उपयोग करना, जो IllegalStateException फेंकता है; इसके बजाय एक नया stream बनाएं।
  2. सिर्फ filter और map के साथ एक pipeline बनाना बिना किसी terminal operation के, जिससे कुछ नहीं चलता क्योंकि intermediate operations lazy हैं।
  3. किसी parallel stream के lambda से एक shared ArrayList में results जोड़ना, जो thread-safe नहीं है; इसके बजाय collect का उपयोग करें।
🔒

Chapter Quiz — Complete all 6 topics to unlock

0/6 topics done

Complete these topics first:

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.