Is lesson me hum seekhenge:
- Stream kya hota hai
- filter() method
- reduce() method
- Real examples
- Functional programming flow
Stream ka matlab:
data ko process karna (collection par)
Example:
filter → transform → result
filter() use hota hai:
condition ke basis par data select karne ke liye
stream.filter(condition)import java.util.*;
class Test {
public static void main(String[] args){
List<Integer> list = Arrays.asList(1,2,3,4,5,6);
list.stream()
.filter(n -> n % 2 == 0)
.forEach(System.out::println);
}
}Output:
2
4
6
list.stream()
.filter(n -> n > 2)
.filter(n -> n % 2 == 0)
.forEach(System.out::println);reduce() ka use hota hai:
multiple values ko ek single result me convert karna
stream.reduce(identity, accumulator)int sum = list.stream()
.reduce(0, (a, b) -> a + b);
System.out.println(sum);Output:
21
int max = list.stream()
.reduce(Integer.MIN_VALUE, (a, b) -> a > b ? a : b);
System.out.println(max);int sum = list.stream()
.filter(n -> n % 2 == 0)
.reduce(0, (a, b) -> a + b);
System.out.println(sum);Output:
12
List → Stream → Filter → Reduce → Result
students list:
filter → pass students
reduce → total marks
✔ filter → condition based selection
✔ reduce → final result
✔ dono functional programming ka core hai
✔ clean code
✔ less loops
✔ readable
✔ parallel processing possible
- filter() kya karta hai?
- reduce() kya hota hai?
- reduce ka identity kya hota hai?
- filter aur map me difference?
Is lesson me humne seekha:
✔ Stream concept
✔ filter() method
✔ reduce() method
✔ combined usage
✔ real examples
Filter & Reduce Java me data processing ko simple aur powerful banate hain.