Java के Collectors
In this page:
stream.collect(Collectors.toList());
stream.collect(Collectors.toSet());
stream.collect(Collectors.groupingBy(item -> key));
stream.collect(Collectors.joining(", "));
What is a Collector?
एक Collector किसी Stream pipeline में terminal operation के रूप में उपयोग होता है, और इसका काम stream के elements को किसी अंतिम container में इकट्ठा करना है, जैसे List, Set, या Map, इस पर निर्भर करते हुए कि आप कौन सा Collector चुनते हैं।
उदाहरण: What is a Collector?
// 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<String> names = Stream.of("Ana", "Bob", "Cy")
.collect(Collectors.toList());
// Print a line to the console
System.out.println(names);
}
}
Login to try C/C++/Java/PHP code in the editor
Collecting into Maps
आप Collectors.toMap() का उपयोग करके stream elements को सीधे एक Map में इकट्ठा कर सकते हैं, जिसके लिए आपको एक key-mapper function और एक value-mapper function दोनों देने होंगे जो बताते हैं कि किसी stream element से हर map entry कैसे निकाली जाए।
उदाहरण: Collecting into Maps
// 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<String> names = List.of("Ana", "Bob", "Cy");
// Create a stream from the collection to process it step by step
Map<String, Integer> lengths = names.stream()
.collect(Collectors.toMap(name -> name, String::length));
// Print a line to the console
System.out.println(lengths);
}
}
Login to try C/C++/Java/PHP code in the editor
Grouping Data
आप Collectors.groupingBy() collector का उपयोग करके stream elements को किसी shared attribute के आधार पर group कर सकते हैं, जो एक ऐसा Map return करता है जिसकी keys grouping categories हैं और जिनकी values हर group के elements की lists हैं।
उदाहरण: Grouping Data
// 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<String> words = List.of("apple", "banana", "avocado", "blueberry");
// Create a stream from the collection to process it step by step
Map<Character, List<String>> grouped = words.stream()
.collect(Collectors.groupingBy(w -> w.charAt(0)));
// Print a line to the console
System.out.println(grouped);
}
}
Login to try C/C++/Java/PHP code in the editor
Partitioning Data
Partitioning grouping का एक special case है जहां grouping key हमेशा एक boolean होती है, data को बिल्कुल दो buckets में बांटते हुए। stream elements को एक ही pass में एक true list और एक false list में बांटने के लिए किसी predicate के साथ Collectors.partitioningBy() का उपयोग करें।
उदाहरण: Partitioning Data
// 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
Map<Boolean, List<Integer>> partitioned = nums.stream()
.collect(Collectors.partitioningBy(n -> n % 2 == 0));
// Print a line to the console
System.out.println(partitioned);
}
}
Login to try C/C++/Java/PHP code in the editor
Joining and Summarizing
आप Collectors.joining() का उपयोग करके string elements को एक delimited string में जोड़ सकते हैं, optionally एक delimiter, prefix, और suffix देते हुए। counts, sums, या averages जैसे running statistics compute करने के लिए, इसके बजाय Collectors.summarizingInt()-style collector का उपयोग करें।
उदाहरण: Joining and Summarizing
// 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<String> names = List.of("Ana", "Bob", "Cy");
// Create a stream from the collection to process it step by step
String joined = names.stream().collect(Collectors.joining(", ", "[", "]"));
// Create a stream from the collection to process it step by step
IntSummaryStatistics stats = names.stream()
.collect(Collectors.summarizingInt(String::length));
// Print a line to the console
System.out.println(joined + " avg=" + stats.getAverage());
}
}
Login to try C/C++/Java/PHP code in the editor
Collectors.toMapका उपयोग करना जब दो elements एक जैसी key produce करें, जोIllegalStateExceptionफेंकता है; तीसरे argument के रूप में एक merge function पास करें।Collectors.toMapमें null value होना, जोNullPointerExceptionफेंकता है।- यह उम्मीद करना कि
groupingByinsertion order बनाए रखेगा, जबकि यह default रूप से एकHashMapreturn करता है; अगर order मायने रखता है तो एकLinkedHashMapयाTreeMapsupplier पास करें।
- Lambda expressions और method references behavior लिखने का एक छोटा तरीका देते हैं।
- Functional interfaces और Stream API filtering और mapping जैसे operations से data process करते हैं।
Optionalउन values को handle करता है जो गायब हो सकती हैं, औरCollectorsstream results इकट्ठा करते हैं।
Chapter Quiz — Complete all 6 topics to unlock
0/6 topics done
Complete these topics first: