Collectors and reduction java In Hindi
जावा प्रोग्रामिंग लैंग्वेज स्ट्रीम एपीआई में, Collectors और Reduction मेथड का यूज़ जावा में स्ट्रीम कलेक्शन ऐरे, लिस्ट, सेट डाटा टाइप एलिमेंट्स को मर्ज या कम्बाइंड करने और इसके फ़ाइनल रिज़ल्ट को डिस्प्ले करने में किया जाता है. जावा यूजर द्वारा कलेक्टर्स और रिडक्शन सपोर्टेड फंक्शन मेथड का यूज़ मल्टीप्ल जावा स्ट्रीम ऑपरेशन टास्क को परफॉर्म करने में किया जाता है।

Collectors Method with Java Streams.
जावा प्रोग्राम में कलेक्टर्स मेथड को यूज़ करने के लिए जावा इसके यूजर को Collectors, java.util.stream नाम से एक बिल्ट-इन रेडीमेड यूटिलिटी क्लास प्रोवाइड करता है. जो किसी जावा प्रोग्राम में स्ट्रीम डाटा टाइप एलिमेंट्स को लिस्ट, सेट्स, मैप्स, स्ट्रिंग्स और अन्य कलेक्शन डाटा टाइप के रिज़ल्ट्स को कलेक्ट करने में हेल्प करता है।
Import these required class package libraries.
import java.util.stream.Collectors;
collect(toList()) Streams function method with Java.
collect(toList()) फंक्शन मेथड जावा स्ट्रीम प्रोग्राम में कलेक्शन ऐरे डाटा टाइप एलिमेंट्स को एक लिस्ट में कलेक्ट कर डिस्प्ले करता है।
Example of the collect(toList()) Streams function.
List<Integer> integers =
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
public class Main {
public static void main(String[] args) {
List<Integer> integers = Arrays.asList(1, 4, 7, 10, 17, 21, 29, 39, 5);
List<Integer> output = integers.stream()
.filter(integer -> integer > 10)
.collect(Collectors.toList());
System.out.println(output);
}
}
Its output.
[17, 21, 29, 39]
collect(toSet()) Streams function method with Java.
collect(toSet()) फंक्शन जावा स्ट्रीम प्रोग्राम में कलेक्शन ऐरे डाटा एलिमेंट में से हर डुप्लिकेट वैल्यू को हटाता है, और एक नए Set डाटा टाइप एलिमेंट की नई लिस्ट को क्रिएट करता है।
Example of the collect(toSet()) Streams function.
List<Integer> integers =
import java.util.Arrays;
import java.util.Set;
import java.util.stream.Collectors;
public class Main {
public static void main(String[] args) {
var integers = Arrays.asList(1, 4, 1, 7, 9, 7, 10, 4, 21, 77);
Set<Integer> output = integers.stream()
.collect(Collectors.toSet());
System.out.println(output);
}
}
Its output.
[1, 4, 21, 7, 9, 10, 77]
joining() Streams function method with Java.
joining() फंक्शन जावा स्ट्रीम प्रोग्राम में कलेक्शन ऐरे डाटा एलिमेंट स्ट्रिंग को एक सिंगल स्ट्रिंग में ग्रुप या कंबाइन करने में यूज़ किया जाता है।
Example of the joining() Streams function.
List<String> city =
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
public class Main {
public static void main(String[] args) {
List<String> city = Arrays.asList(“Mumbai”, “Delhi”, “Bengaluru”, “Hyderabad”, “Ahmedabad”);
String output = city.stream()
.collect(Collectors.joining(“: “));
System.out.println(output);
}
}
Its output.
Mumbai: Delhi: Bengaluru: Hyderabad: Ahmedabad
groupingBy() Streams function method with Java.
groupingBy() फंक्शन जावा स्ट्रीम प्रोग्राम में कलेक्शन ऐरे डाटा टाइप एलिमेंट्स को किसी पर्टिकुलर स्पेसिफिक एंगेल प्रॉपर्टी के अनुसार ग्रुप कर डिस्प्ले करता है।
Example of the groupingBy() Streams function.
List<String> country =
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
public class Main {
public static void main(String[] args) {
List<String> country = Arrays.asList(“India”, “United States”, “China”, “Canada”, “Brazil”, “Australia”, “Argentina”);
Map<Integer, List<String>> output = country.stream()
.collect(Collectors.groupingBy(String::length));
System.out.println(output);
}
}
Its output.
{5=[India, China], 6=[Canada, Brazil], 9=[Australia, Argentina], 13=[United States]}
यह फंक्शन कंट्री नामों को उनकी लेंथ के हिसाब से ग्रुप कर डिस्प्ले करता है।
counting() Streams function method with Java.
counting() फंक्शन जावा स्ट्रीम प्रोग्राम में कलेक्शन ऐरे डाटा टाइप एलिमेंट्स को काउंट करता है, ज्यादातर जावा स्ट्रीम में इसे groupingBy() फंक्शन मेथड के साथ यूज़ किया जाता है।
Example of the counting() Streams function.
Map<Integer, Long> output =
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
public class Main {
public static void main(String[] args) {
List<String> programming = Arrays.asList(“Rust”, “Pearl”, “Matlab”, “Mysql”, “Jquery”, “Html”);
Map<Integer, Long> output = programming.stream()
.collect(Collectors.groupingBy(
String::length,
Collectors.counting()
));
System.out.println(output);
}
}
Its output.
{4=2, 5=2, 6=2}
Reduction Method with Java Streams.
जावा प्रोग्राम में रिडक्शन स्ट्रीम कांसेप्ट का मतलब है, की मल्टीप्ल अवेलेबल स्ट्रीम कलेक्शन डाटा टाइप एलिमेंट वैल्यू को एक ही रिज़ल्ट में ग्रुप कर डिस्प्ले करना।
Common reduction operations in a Java program include.
- sum() reduction function.
- count() reduction function.
- min() reduction function.
- max() reduction function.
- reduce() reduction function.
reduce() Streams function method with Java.
जावा प्रोग्राम में reduce() फंक्शन मेथड कलेक्शन डाटा टाइप एलिमेंट को स्टेप बाय स्टेप ऐड कर डिस्प्ले करता है।
Example of the reduce() Streams function.
List<Integer> integers =
import java.util.Arrays;
import java.util.List;
public class Main {
public static void main(String[] args) {
List<Integer> integers = Arrays.asList(1, 3, 7, 11, 17, 24, 39, 12);
int sum = integers.stream()
.reduce(0, (p, q) -> p + q);
System.out.println(sum);
}
}
Its output.
114
Its calculation is as follows.
0 + 1 = 1
1 + 3 = 4
4 + 7 = 11
11 + 11 = 22
22 + 17 = 39
39 + 24 = 63
63 + 39 = 102
102 + 12 = 114
Finding Maximum Streams function method with Java.
जावा प्रोग्राम में max() फंक्शन मेथड कलेक्शन डाटा टाइप एलिमेंट में से एक लार्ज मैक्सिमम वैल्यू एलिमेंट को फाइंड करने में यूज़ किया जाता है।
Example of the max() Streams function.
int max =
import java.util.Arrays;
import java.util.List;
public class Main {
public static void main(String[] args) {
List<Integer> integers = Arrays.asList(1, 3, 9, 2, 7, 9, 5, 6, 11);
int max = integers.stream()
.reduce(Integer.MIN_VALUE, Integer::max);
System.out.println(max);
}
}
Its output.
11
Searching Products Streams function method with Java.
जावा प्रोग्राम में product() फंक्शन मेथड कलेक्शन डाटा टाइप एलिमेंट के प्रोडक्ट या मल्टीप्लय वैल्यू को फाइंड करने में यूज़ किया जाता है।
Example of the product Streams function.
int product =
import java.util.Arrays;
import java.util.List;
public class Main {
public static void main(String[] args) {
List<Integer> integers = Arrays.asList(3, 3, 2, 7, 5);
int product = integers.stream()
.reduce(1, (p, q) -> p * q);
System.out.println(product);
}
}
Its output.
630
Its calculation is as follows.
[3, x 3, x 2, x 7, x 5] = 630
Main difference between Collectors vs Reduction in java stream
| # | Collectors stream java method | Reduction stream java method |
| 1. | Collectors’ java method used to collect/process array, list, set, map data elements into an output result | Reduction java method used to combine array, list, set, map data elements into one final output result |
| 2. | Generally, we used it with collect() function method | Normally we can usually uses reduce() function method for it |
| 3. | With collectors’ data type we can create list, set, map, string, data type element collection | With reduce function usually we can produce a single value output as result |
| 4. | Collectors used to display a list of collection tolist() data element | We can calculate a number of sums using reduce() function with given integer values |
| 5. | Collectors used to display a set of collection toset() data element | We can find a number of products using reduce() function with given integer values |
| 6. | Used to display a group of collection groupingby() data element | We can use it to find a number of only 1 single maximum value using reduce() function among given integer values |
| 7. | Used to display or join a group of collection joining() data element | We can use it to find a number of minimum values using reduce() function among given integer values |
Conclusion of Collector vs. Reduction method in Java streams.
जावा में Collector vs. Reduction मेथड को आसानी से याद रखने का एक आसान तरीका है।
- Collector stream method → यह यूजर डिफाइन जावा प्रोग्राम कलेक्शन डाटा टाइप के “नतीजे को इकट्ठा करें”
- Reduction stream method → यह यूजर डिफाइन जावा प्रोग्राम डाटा के रिजल्ट को ग्रुप “एक नतीजे में मिलाएं”
Collector vs. Reduction stream method concept example.
integers.stream()
.collect(Collectors.toList());
इसका का मतलब है, यहाँ कलेक्शन डाटा के एलिमेंट को एक लिस्ट में इकट्ठा करना है।
Whereas.
integers.stream()
.reduce(0, Integer::sum);
इसका का मतलब है, यहाँ कलेक्शन डाटा के एलिमेंट को एक लिस्ट में एक वैल्यू में मिलान करता है.

