Functional Programming with Java Tutorial
Selected Reading
- Discussion
- Resources
- Quick Guide
- Fixed Length Streams
- Infinite Streams
- Terminal methods
- Intermediate Methods
- Exception Handling
- Type Inference
- Pure Functions
- First Class Functions
- Returning a Function
- High Order Functions
- Collections
- Constructor References
- Method References
- Functional Interfaces
- Default Methods
- Lambda Expressions
- Reducing
- Currying
- Closure
- Optionals & Monads
- Parallelism
- Recursion
- Persistent Data Structure
- Eager vs Lazy Evaluation
- Functional Composition
- Functions
- Overview
- Home
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
Fixed Length Streams
Functional Programming - Fixed length Streams
There are multiple ways using which we can create fix length streams.
Using Stream.of() method
Using Collection.stream() method
Using Stream.builder() method
Following example shows all of the above ways to create a fix length stream.
Example - Fix Length Stream
import java.util.Arrays; import java.util.List; import java.util.stream.Stream; pubpc class FunctionTester { pubpc static void main(String[] args) { System.out.println("Stream.of():"); Stream<Integer> stream = Stream.of(1, 2, 3, 4, 5); stream.forEach(System.out::println); System.out.println("Collection.stream():"); Integer[] numbers = {1, 2, 3, 4, 5}; List<Integer> pst = Arrays.asList(numbers); pst.stream().forEach(System.out::println); System.out.println("StreamBuilder.build():"); Stream.Builder<Integer> streamBuilder = Stream.builder(); streamBuilder.accept(1); streamBuilder.accept(2); streamBuilder.accept(3); streamBuilder.accept(4); streamBuilder.accept(5); streamBuilder.build().forEach(System.out::println); } }
Output
Stream.of(): 1 2 3 4 5 Collection.stream(): 1 2 3 4 5 StreamBuilder.build(): 1 2 3 4 5Advertisements