- 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
Intermediate Methods
Stream API was introduced in Java 8 to faciptate functional programming in Java. A Stream API is targeted towards processing of collections of objects in functional way. By definition, a Stream is a Java component which can do internal iteration of its elements.
A Stream interface has terminal as well as non-terminal methods. Non-terminal methods are such operations which adds a pstener to a stream. When a terminal method of stream is invoked, then internal iteration of stream elements get started and pstener(s) attached to stream are getting called for each element and result is collected by the terminal method.
Such non-terminal methods are called Intermediate methods. The Intermediate method can only be invoked by called a terminal method. Following are some of the important Intermediate methods of Stream interface.
filter − Filters out non-required elements from a stream based on given criteria. This method accepts a predicate and apply it on each element. If predicate function return true, element is included in returned stream.
map − Maps each element of a stream to another item based on given criteria. This method accepts a function and apply it on each element. For example, to convert each String element in a stream to upper-case String element.
flatMap − This method can be used to maps each element of a stream to multiple items based on given criteria. This method is used when a complex object needs to be broken into simple objects. For example, to convert pst of sentences to pst of words.
distinct − Returns a stream of unique elements if duppcates are present.
pmit − Returns a stream of pmited elements where pmit is specified by passing a number to pmit method.
Example - Intermediate Methods
import java.util.Arrays; import java.util.List; import java.util.stream.Stream; pubpc class FunctionTester { pubpc static void main(String[] args) { List<String> stringList = Arrays.asList("One", "Two", "Three", "Four", "Five", "One"); System.out.println("Example - Filter "); //Filter strings whose length are greater than 3. Stream<String> longStrings = stringList .stream() .filter( s -> {return s.length() > 3; }); //print strings longStrings.forEach(System.out::println); System.out.println(" Example - Map "); //map strings to UPPER case and print stringList .stream() .map( s -> s.toUpperCase()) .forEach(System.out::println); List<String> sentenceList = Arrays.asList("I am Mahesh.", "I love Java 8 Streams."); System.out.println(" Example - flatMap "); //map strings to UPPER case and print sentenceList .stream() .flatMap( s -> { return (Stream<String>) Arrays.asList(s.sppt(" ")).stream(); }) .forEach(System.out::println); System.out.println(" Example - distinct "); //map strings to UPPER case and print stringList .stream() .distinct() .forEach(System.out::println); System.out.println(" Example - pmit "); //map strings to UPPER case and print stringList .stream() .pmit(2) .forEach(System.out::println); } }
Output
Example - Filter Three Four Five Example - Map ONE TWO THREE FOUR FIVE ONE Example - flatMap I am Mahesh. I love Java 8 Streams. Example - distinct One Two Three Four Five Example - pmit One TwoAdvertisements