Stream Processing and Functional Operations on LinkedList in Java 8
Stream Processing and Functional Operations on LinkedList in Java 8
Introduction
Java 8 ने एक बहुत ही powerful concept introduce किया था — Stream API। ये concept data को process करने का modern और functional तरीका देता है। जब हम LinkedList के साथ काम करते हैं, तो Stream API हमें data को filter, sort, transform और reduce करने की सुविधा देता है, वो भी बहुत कम कोड में।
Stream processing का main उद्देश्य है data को sequential या parallel तरीके से process करना, ताकि performance और readability दोनों maintain रहें।
What is Stream API in Java 8?
Stream API एक ऐसी feature है जो collections जैसे ArrayList, LinkedList या arrays पर bulk data operations करने के लिए use होती है। ये operations functional style में होते हैं, यानी हमें loops या conditional statements की जरूरत नहीं होती।
Stream API में दो types के operations होते हैं:
- Intermediate Operations: जैसे
filter(),map(),sorted()— ये new stream return करते हैं। - Terminal Operations: जैसे
forEach(),collect(),count()— ये final result produce करते हैं।
Using Stream API with LinkedList
जब हम LinkedList के साथ Stream API use करते हैं, तो हमें data को manipulate करने के लिए आसान और readable syntax मिलता है। पहले हम traditional loop से iterate करते थे, लेकिन अब हम एक ही line में पूरा transformation कर सकते हैं।
Example: Convert LinkedList to Stream
LinkedList<Integer> list = new LinkedList<>();
list.add(10);
list.add(20);
list.add(30);
list.stream().forEach(System.out::println);
ऊपर के example में list.stream() के जरिए LinkedList को stream में convert किया गया और फिर forEach() से data print किया गया।
Functional Operations on LinkedList using Stream API
1. Filtering Data
Filter operation का use किसी specific condition के आधार पर elements select करने के लिए किया जाता है।
LinkedList<Integer> list = new LinkedList<>(Arrays.asList(5,10,15,20,25));
list.stream()
.filter(num -> num > 10)
.forEach(System.out::println);
यहाँ filter() ने केवल उन values को select किया जो 10 से बड़ी हैं।
2. Mapping Elements
map() function elements को transform करने के लिए use होता है — यानी हर element पर operation apply होता है और result में new stream मिलती है।
LinkedList<String> names = new LinkedList<>(Arrays.asList("java","python","c++"));
names.stream()
.map(String::toUpperCase)
.forEach(System.out::println);
यहाँ हर string को uppercase में convert किया गया है।
3. Sorting Stream Data
sorted() method stream data को ascending या custom order में arrange करता है।
LinkedList<Integer> list = new LinkedList<>(Arrays.asList(50,10,30,20,40));
list.stream()
.sorted()
.forEach(System.out::println);
यह code LinkedList के elements को sorted order में print करता है।
4. Collecting Stream Results
collect() method stream को वापस किसी collection या single result में convert करता है।
LinkedList<Integer> list = new LinkedList<>(Arrays.asList(5,10,15,20));
List<Integer> evenList = list.stream()
.filter(num -> num % 2 == 0)
.collect(Collectors.toList());
System.out.println(evenList);
इस code में filter() से even numbers चुने गए और collect() से उन्हें नई list में store किया गया।
5. Reducing Elements
reduce() operation stream के elements को एक single value में combine करता है — जैसे sum, average या concatenation।
LinkedList<Integer> list = new LinkedList<>(Arrays.asList(1,2,3,4,5));
int sum = list.stream()
.reduce(0, (a,b) -> a + b);
System.out.println(sum);
यह code LinkedList के सभी numbers का sum निकालता है।
Stream Pipeline Concept
Stream operations एक pipeline बनाते हैं, जिसमें data step-by-step process होता है।
- Source: जैसे LinkedList या ArrayList
- Intermediate operations: जैसे filter(), map(), sorted()
- Terminal operation: जैसे collect(), forEach()
जब तक terminal operation call नहीं किया जाता, intermediate operations execute नहीं होते।
Parallel Streams in LinkedList
Parallel stream multiple threads का use करके elements को parallel process करता है। इससे performance improve होती है, खासकर large datasets में।
LinkedList<Integer> list = new LinkedList<>(Arrays.asList(1,2,3,4,5,6,7,8,9));
list.parallelStream()
.forEach(num -> System.out.println(Thread.currentThread().getName() + " - " + num));
यहाँ हर element अलग-अलग thread से process होता है, जिससे speed बढ़ती है।
Benefits of Using Stream API with LinkedList
- Less code और high readability
- Functional style programming
- Parallel processing support
- Efficient data manipulation
- Reusable and flexible operations
Traditional vs Stream Approach
| Traditional Loop | Stream API |
|---|---|
| Manual iteration using for loop | Automatic iteration with functional methods |
| More lines of code | Less and clean code |
| No parallel processing | Supports parallel execution |
| Low readability | High readability |
Real-world Examples of Stream Operations
Example 1: Student Marks Filtering
LinkedList<Integer> marks = new LinkedList<>(Arrays.asList(45, 67, 89, 32, 76));
marks.stream()
.filter(m -> m >= 50)
.forEach(System.out::println);
यह example उन students को दिखाता है जिनके marks 50 या उससे ज्यादा हैं।
Example 2: Convert Names to Uppercase and Sort
LinkedList<String> names = new LinkedList<>(Arrays.asList("Ankit", "Rahul", "Pooja", "Neha"));
names.stream()
.map(String::toUpperCase)
.sorted()
.forEach(System.out::println);
Example 3: Count Specific Elements
LinkedList<String> words = new LinkedList<>(Arrays.asList("java","python","java","c","java"));
long count = words.stream()
.filter(w -> w.equals("java"))
.count();
System.out.println(count);
यह code "java" शब्द कितनी बार आया है, यह count करता है।
Best Practices for Using Stream API
- Stream को re-use करने से बचें — हर बार नई stream बनाएं।
- Null values से बचने के लिए पहले validation करें।
- Parallel stream का use तभी करें जब data बड़ा हो।
- Functional operations को short और clear रखें।
- Performance-critical tasks में profiling ज़रूर करें।
Exam Notes (Quick Revision)
- Stream API – introduced in Java 8 for functional-style data processing।
- Intermediate Operations – filter(), map(), sorted()
- Terminal Operations – forEach(), collect(), reduce()
- Stream Source – LinkedList, ArrayList, Arrays
- Parallel Stream – multi-threaded data processing
- Functional Approach – clean, concise, readable code
- filter() – select elements based on condition
- map() – transform elements
- sorted() – sort data
- reduce() – combine elements into one result