Java का Stream API
In this page:
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?
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);
}
}
Login to try C/C++/Java/PHP code in the editor
Filter and Map
filter() operation सिर्फ वे elements चुनता है जो किसी दी गई condition से मेल खाते हों, बाकी को pipeline से हटाते हुए। map() operation इसके बजाय stream के हर element को एक नई value में बदल देता है, एक-के-लिए-एक, मौजूद elements की संख्या बदले बिना।
उदाहरण: Filter and Map
// 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);
}
}
Login to try C/C++/Java/PHP code in the editor
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
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);
}
}
Login to try C/C++/Java/PHP code in the editor
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
// 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);
}
}
Login to try C/C++/Java/PHP code in the editor
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
// 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);
}
}
Login to try C/C++/Java/PHP code in the editor
- count() जैसे किसी terminal operation के बाद किसी stream को फिर से उपयोग करना, जो
IllegalStateExceptionफेंकता है; इसके बजाय एक नया stream बनाएं। - सिर्फ
filterऔरmapके साथ एक pipeline बनाना बिना किसी terminal operation के, जिससे कुछ नहीं चलता क्योंकि intermediate operations lazy हैं। - किसी 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: