Subjects

All subjects Django Java Python React Spring Boot JavaScript PHP
Sign Up Free
Interview question

What are common Stream terminal operations like collect(), reduce(), and forEach()? collect(), reduce(), forEach() जैसे common Stream terminal operations क्या हैं?

Answer

Terminal operations trigger the actual execution of a stream pipeline and produce a result (or side effect), after which the stream cannot be reused.

import java.util.stream.*;

List<Integer> numbers = List.of(1, 2, 3, 4, 5);

// collect() - accumulates elements into a collection or other structure
List<Integer> evenList = numbers.stream()
    .filter(n -> n % 2 == 0)
    .collect(Collectors.toList());
System.out.println(evenList);  // [2, 4]

Set<Integer> evenSet = numbers.stream()
    .filter(n -> n % 2 == 0)
    .collect(Collectors.toSet());

String joined = numbers.stream()
    .map(String::valueOf)
    .collect(Collectors.joining(', ', '[', ']'));
System.out.println(joined);  // [1, 2, 3, 4, 5]

// reduce() - combines elements into a single value
int sum = numbers.stream()
    .reduce(0, (a, b) -> a + b);  // identity value + combining function
System.out.println(sum);  // 15

Optional<Integer> max = numbers.stream()
    .reduce((a, b) -> a > b ? a : b);  // no identity - returns Optional
System.out.println(max.get());  // 5

// forEach() - performs a side-effecting action per element, returns nothing
numbers.stream().forEach(n -> System.out.println('Number: ' + n));

// Other useful terminal operations
long count = numbers.stream().filter(n -> n > 2).count();  // 3
boolean anyEven = numbers.stream().anyMatch(n -> n % 2 == 0);  // true
boolean allPositive = numbers.stream().allMatch(n -> n > 0);   // true
Optional<Integer> first = numbers.stream().findFirst();         // Optional[1]

// Collectors.groupingBy - grouping elements by a classifier function
Map<Boolean, List<Integer>> groups = numbers.stream()
    .collect(Collectors.groupingBy(n -> n % 2 == 0));
System.out.println(groups);  // {false=[1, 3, 5], true=[2, 4]}

Terminal operations stream pipeline के actual execution को trigger करते हैं और result देते हैं, इसके बाद stream reuse नहीं हो सकता।

import java.util.stream.*;

List<Integer> numbers = List.of(1, 2, 3, 4, 5);

// collect() - collection में accumulate करता है
List<Integer> evenList = numbers.stream()
    .filter(n -> n % 2 == 0)
    .collect(Collectors.toList());
System.out.println(evenList);  // [2, 4]

String joined = numbers.stream()
    .map(String::valueOf)
    .collect(Collectors.joining(', ', '[', ']'));
System.out.println(joined);  // [1, 2, 3, 4, 5]

// reduce() - एक single value में combine करता है
int sum = numbers.stream()
    .reduce(0, (a, b) -> a + b);
System.out.println(sum);  // 15

Optional<Integer> max = numbers.stream()
    .reduce((a, b) -> a > b ? a : b);
System.out.println(max.get());  // 5

// forEach() - हर element पर side-effect action
numbers.stream().forEach(n -> System.out.println('Number: ' + n));

long count = numbers.stream().filter(n -> n > 2).count();
boolean anyEven = numbers.stream().anyMatch(n -> n % 2 == 0);
boolean allPositive = numbers.stream().allMatch(n -> n > 0);

Map<Boolean, List<Integer>> groups = numbers.stream()
    .collect(Collectors.groupingBy(n -> n % 2 == 0));
System.out.println(groups);  // {false=[1, 3, 5], true=[2, 4]}

Was this answer clear?