Feedback Form

Intermediate Operations: filter(), map(), flatMap(), sorted(), distinct()

Intermediate Operations in Java Stream: filter(), map(), flatMap(), sorted(), distinct()

जब हम Java Stream API के साथ काम करते हैं, तो Intermediate Operations बहुत ही important role निभाते हैं। ये operations हमें data को process करने, transform करने और filter करने की flexibility देते हैं बिना original data को modify किए।

Java में Stream API का use functional style programming लाने के लिए किया गया था, जिससे हम collections या arrays के data को declarative तरीके से process कर सकें। Intermediate operations वो होते हैं जो एक नया Stream return करते हैं और chain में आगे process किए जा सकते हैं।

What are Intermediate Operations?

Intermediate operations वो functions होते हैं जो Stream को process करते हैं लेकिन final result produce नहीं करते। ये operations lazy nature के होते हैं — यानी जब तक terminal operation apply नहीं किया जाता, तब तक ये execute नहीं होते।

  • ये हमेशा एक Stream return करते हैं।
  • Multiple intermediate operations chain किए जा सकते हैं।
  • Execution तभी होता है जब terminal operation call किया जाए।

Common intermediate operations हैं: filter(), map(), flatMap(), sorted(), और distinct()। चलिए इन्हें एक-एक करके detail में समझते हैं।

filter() Operation

filter() method का use Stream के elements को किसी condition के अनुसार filter करने के लिए किया जाता है। यह method एक predicate (boolean condition) लेता है और वही elements return करता है जो उस condition को satisfy करते हैं।

Syntax

Stream filter(Predicate predicate)

Example

List numbers = Arrays.asList(10, 25, 30, 45, 50);
List evenNumbers = numbers.stream()
.filter(n -> n % 2 == 0)
.collect(Collectors.toList());
System.out.println(evenNumbers);

ऊपर के example में filter() सिर्फ even numbers को चुनता है और बाकी को ignore करता है।

Key Points

  • filter() का use conditional data selection के लिए किया जाता है।
  • Predicate interface के साथ काम करता है।
  • Original list को modify नहीं करता।

map() Operation

map() operation हर element को transform करता है — मतलब एक function apply करके elements को नया shape देता है। इसका use तब होता है जब आपको data को modify या convert करना हो।

Syntax

<R> Stream<R> map(Function<? super T, ? extends R> mapper)

Example

List names = Arrays.asList("ravi", "amit", "sita");
List upperNames = names.stream()
.map(String::toUpperCase)
.collect(Collectors.toList());
System.out.println(upperNames);

यहाँ map() हर string को uppercase में convert कर रहा है।

Key Points

  • map() transformation के लिए use होता है।
  • हर element पर function apply करता है।
  • Functional interface Function<T, R> को use करता है।

flatMap() Operation

flatMap() थोड़ा advanced operation है जो nested structure वाले data को flatten करता है। यानी अगर आपके पास list of lists है, तो उसे single stream में convert करता है।

Syntax

<R> Stream<R> flatMap(Function<? super T, ? extends Stream<? extends R>> mapper)

Example

List<List<String>> data = Arrays.asList(
Arrays.asList("A", "B"),
Arrays.asList("C", "D"),
Arrays.asList("E", "F")
);

List<String> flatList = data.stream()
.flatMap(List::stream)
.collect(Collectors.toList());
System.out.println(flatList);

यहाँ flatMap() ने 2D list को single list में बदल दिया।

Key Points

  • Nested structure को flatten करने के लिए use होता है।
  • map() की तरह ही है लेकिन return type Stream होता है।
  • Functional programming में बहुत useful है।

sorted() Operation

sorted() method का use Stream elements को sorting order में arrange करने के लिए किया जाता है। ये दो forms में आता है — natural order और custom comparator के साथ।

Syntax

Stream<T> sorted()
Stream<T> sorted(Comparator<? super T> comparator)

Example 1 (Natural Order)

List<Integer> numbers = Arrays.asList(5, 2, 8, 1, 3);
List<Integer> sortedList = numbers.stream()
.sorted()
.collect(Collectors.toList());
System.out.println(sortedList);

Example 2 (Custom Comparator)

List<String> names = Arrays.asList("Ravi", "Amit", "Sita");
List<String> descNames = names.stream()
.sorted((a, b) -> b.compareTo(a))
.collect(Collectors.toList());
System.out.println(descNames);

पहले example में natural sorting हुई है जबकि दूसरे में reverse order में।

Key Points

  • sorted() data को ascending या descending order में sort करता है।
  • Comparator के साथ custom logic भी दिया जा सकता है।
  • Original data unaffected रहता है।

distinct() Operation

distinct() method duplicate elements को remove करने के लिए use किया जाता है। ये operation Stream के सभी unique elements return करता है।

Syntax

Stream<T> distinct()

Example

List<Integer> numbers = Arrays.asList(10, 20, 10, 30, 20, 40);
List<Integer> unique = numbers.stream()
.distinct()
.collect(Collectors.toList());
System.out.println(unique);

यहाँ distinct() ने duplicate values को हटा दिया और केवल unique numbers दिखाए।

Key Points

  • distinct() duplicate elements हटाने के लिए use होता है।
  • Equality check के लिए equals() method का use करता है।
  • Stream को unique values के साथ return करता है।

Combining Multiple Intermediate Operations

Stream API की सबसे बड़ी power यही है कि हम multiple intermediate operations को chain करके complex data processing कर सकते हैं।

Example

List<String> names = Arrays.asList("ravi", "amit", "sita", "ravi", "sunil");
List<String> result = names.stream()
.filter(n -> n.length() > 3)
.map(String::toUpperCase)
.distinct()
.sorted()
.collect(Collectors.toList());
System.out.println(result);

इस code में हमने एक साथ filter(), map(), distinct() और sorted() का use किया है जिससे readable और clean data processing हुई।

Output

[AMIT, RAVI, SITA, SUNIL]

Performance Tips for Intermediate Operations

  • Operations को सही sequence में arrange करें — पहले filter(), फिर map() और अंत में sorted()।
  • Unnecessary intermediate steps avoid करें ताकि performance बेहतर रहे।
  • Parallel streams का use तब करें जब data बहुत large हो।

Comparison Table of Intermediate Operations

Operation Purpose Return Type Use Case
filter() Condition के अनुसार elements को चुनना Stream<T> Even numbers, specific data selection
map() Elements को transform करना Stream<R> Uppercase, data conversion
flatMap() Nested data को flatten करना Stream<R> List of lists को single list में बदलना
sorted() Elements को sort करना Stream<T> Ascending/Descending order में sorting
distinct() Duplicate elements को हटाना Stream<T> Unique values पाना

Real-Life Use Cases

Intermediate operations का use सिर्फ theory तक सीमित नहीं है, बल्कि ये real-world applications में भी बहुत काम आते हैं।

  • filter(): किसी company के employee list में सिर्फ active employees दिखाना।
  • map(): student list से उनके names को uppercase में दिखाना।
  • flatMap(): department-wise employee lists को merge करना।
  • sorted(): salary या marks के हिसाब से sorting करना।
  • distinct(): duplicate email IDs हटाना।

Exam Important Notes

  • filter() → condition के अनुसार data choose करता है।
  • map() → elements को transform करता है।
  • flatMap() → nested streams को flatten करता है।
  • sorted() → data को order में arrange करता है।
  • distinct() → duplicate elements remove करता है।
  • Intermediate operations lazy होते हैं — जब तक terminal operation नहीं चलता, execution नहीं होता।
  • हर operation original data को modify नहीं करता, बल्कि new stream बनाता है।
  • Stream API functional programming style को promote करता है।