SlideShare a Scribd company logo
Concept of Stream API Java 1.8
Stream concept is introduced in java 1.8 and present in java.util.stream
package.It is used to process the object from collection or any group of objects
or data source. We can easily understand java stream concept by it's name
stream, In stream water flows from one water source to destination and we can
perform some operations like filtering, collecting etc on stream to get useful
water,Same in java stream concept we can think object flows from one object
source to destination by Stream pipelines (A stream pipeline is composed of a
stream source, zero or more intermediate operations, and a terminal
operation.)
We should be familiar with some concept before going to further in stream
concept and remember one thing streams don’t actually store elements; they
are computed on demand.
1> C.stream() - we are going to get an stream object where C is collection
object.
2>filter()- used to do filtering based on predicate(predicate is nothing but
boolean condition, basically it is a functional interface so it can be replaced by
lambda expression)
3> collect()- it is a terminal operation use to collect filtered or mapped data.
Example-
ArrayList<Integer> arrayList = new ArrayList<Integer>();
arrayList.add(0);
arrayList.add(10);
arrayList.add(20);
arrayList.add(5);
arrayList.add(15);
arrayList.add(25);
System.out.println(arrayList);
output will be [0,10,20,5,15,25]. Now,we want to get a new ArrayList of
integer which contains only even integers from existing arrayList object. Here,
how we process collection object without stream concept
List<Integer> list = new ArrayList<Integer>();
for(Integer i:arrayList){
if(i%2 == 0)
list.add(i);
}
System.out.println(arrayList);
output will be [0,10,20]
with stream concept-
List<Integer> list =
arrayList.stream().filter(i->i%2==0).collect(Collectors.toList());
System.out.println(arrayList);
output will be [0,10,20].In collection if we perform bulk operation then highly
recommended to use stream concept.
some more useful method of stream concept-
4> map()- for every object if we want to perform some action and want some
result object then we use map method if we want to increase all the arrayList
data by 5 then we can use this function.
List<Integer> l = arrayList.stream().map(i-> i+5).collect(Collectors.toList());
it always take functional interface(interface which contains only one method)
so we can replace it by lambda expression.
5> count() - use to count how many objects are there in stream ( not in data
source).
long noOfEvenInteger = arrayList.stream().filter(i-> i%2!=0).count();
6> sorted() - use to perform sorting in ascending order.
List<Integer> l = arrayList.stream().sorted().collect(Collectors.toList()); return
an ascending order list.
for customization we go for comparator concept.comparator is also a
functional interface so we can replace it by lambda expression.
Comparator - it contains compare() method and we can replace it by lambda
expression compare(obj1, obj2)- return -ve if obj1 has to come before
obj2; return +ve if obj1 has to come after obj2; return 0 if both are
equal;
we perform sorting in desc order here, what we do inside compare method (i1,
i2)-> (i1<i2)?1:(i1 > i2)?-1:0 . We compare two object i1 and i2 if i1 is smaller
than i2 it means i1 has to come before i2, so it will return +ve else it check i1 is
bigger or not if yes it means i1 has to come after i2, so it will return -ve and if
both are equal then it will return 0;
List<Integer> list = arrayList.stream().sorted((i1,i2)->(i1, i2)-> (i1<i2)?1:(i1 >
i2)?-1:0).collect(Collectors.toList());
7> forEach() - for every element if we want to perform some functionality.
arrayList.stream().forEach(System.out::println);
it will print all object.We can call our own method also.
Consumer<Integer> fun = i->{System.out.println("square of"+i+"is = "+(i*i))};
arrayList.stream().forEach(fun);
8> toArray() - to convert stream of object into array.
Integer[] arr = arrayList.stream().toArray(Integer::new);
9> Stream.of(arr) - use to get stream from array or other group of data;
Stream s = stream.of(10,1,2,12,34);
s.forEach(System.out::println);

More Related Content

DOCX
Array list
PDF
Java ArrayList Tutorial | Edureka
PPT
Engineering lecture ppt by venay magen
PPTX
F# array searching
PDF
Priorty queue
PDF
Stacks,queues,linked-list
PPT
Array Presentation (EngineerBaBu.com)
PDF
Przywitaj się z reactive extensions
Array list
Java ArrayList Tutorial | Edureka
Engineering lecture ppt by venay magen
F# array searching
Priorty queue
Stacks,queues,linked-list
Array Presentation (EngineerBaBu.com)
Przywitaj się z reactive extensions

What's hot (20)

PPTX
Net (f#) array
PDF
Lecture 6 - Arrays
PDF
Python programming : Arrays
PDF
The Ring programming language version 1.5.2 book - Part 21 of 181
PDF
Arrays In Python | Python Array Operations | Edureka
PPTX
Unit 5 linked list
PDF
The Functional Programming Triad of fold, scan and iterate
PDF
The Functional Programming Triad of fold, scan and iterate
PPTX
Python array
PDF
Arrays in python
PPTX
Min priority queue
PPTX
Max priority queue
PDF
ODP
Knolx session
PPTX
Address calculation-sort
PPTX
Data Structures - Lecture 3 [Arrays]
DOC
Array properties
PPT
Unit 3 stack
PPT
Java Presentation
Net (f#) array
Lecture 6 - Arrays
Python programming : Arrays
The Ring programming language version 1.5.2 book - Part 21 of 181
Arrays In Python | Python Array Operations | Edureka
Unit 5 linked list
The Functional Programming Triad of fold, scan and iterate
The Functional Programming Triad of fold, scan and iterate
Python array
Arrays in python
Min priority queue
Max priority queue
Knolx session
Address calculation-sort
Data Structures - Lecture 3 [Arrays]
Array properties
Unit 3 stack
Java Presentation
Ad

Similar to Concept of Stream API Java 1.8 (20)

PPTX
Java Advanced Topic - Streams Presentations
PDF
Harnessing the Power of Java 8 Streams
PPTX
Java 8 Streams And Common Operations By Harmeet Singh(Taara)
PPTX
Java 8 streams
PPT
Java 8 Streams
PDF
Charles Sharp: Java 8 Streams
PDF
Going reactive in java
PDF
Lambda.pdf
PDF
JDK8 Functional API
PDF
Java 8 Stream API. A different way to process collections.
PPTX
Java 8 Lambda and Streams
PDF
Java Lambda internals with invoke dynamic
PPTX
Introduction to java 8 stream api
PPTX
Java 8 stream and c# 3.5
PDF
Java 8 new features or the ones you might actually use
PPTX
Java 8
PDF
Streams in Java 8
PPTX
Java8 training - Class 1
PPTX
PPTX
Java se 8 streams pt1
Java Advanced Topic - Streams Presentations
Harnessing the Power of Java 8 Streams
Java 8 Streams And Common Operations By Harmeet Singh(Taara)
Java 8 streams
Java 8 Streams
Charles Sharp: Java 8 Streams
Going reactive in java
Lambda.pdf
JDK8 Functional API
Java 8 Stream API. A different way to process collections.
Java 8 Lambda and Streams
Java Lambda internals with invoke dynamic
Introduction to java 8 stream api
Java 8 stream and c# 3.5
Java 8 new features or the ones you might actually use
Java 8
Streams in Java 8
Java8 training - Class 1
Java se 8 streams pt1
Ad

More from InnovationM (20)

PDF
How to use data binding in android
PDF
Capture image on eye blink
PDF
Mob x in react
PDF
How to use geolocation in react native apps
PDF
Android 8 behavior changes
PDF
Understanding of react fiber architecture
PDF
Automatic reference counting (arc) and memory management in swift
PDF
Firebase crashlytics integration in iOS swift (dSYM File Required Problem Res...
PDF
How prototype works in java script?
PDF
React – Let’s “Hook” up
PDF
Razorpay Payment Gateway Integration In iOS Swift
PDF
Paytm integration in swift
PDF
Line Messaging API Integration with Spring-Boot
PDF
Basic fundamental of ReactJS
PDF
Basic Fundamental of Redux
PDF
Integration of Highcharts with React ( JavaScript library )
PDF
Serialization & De-serialization in Java
PDF
How to Make Each Round of Testing Count?
PDF
Model View Presenter For Android
PDF
Retrofit Library In Android
How to use data binding in android
Capture image on eye blink
Mob x in react
How to use geolocation in react native apps
Android 8 behavior changes
Understanding of react fiber architecture
Automatic reference counting (arc) and memory management in swift
Firebase crashlytics integration in iOS swift (dSYM File Required Problem Res...
How prototype works in java script?
React – Let’s “Hook” up
Razorpay Payment Gateway Integration In iOS Swift
Paytm integration in swift
Line Messaging API Integration with Spring-Boot
Basic fundamental of ReactJS
Basic Fundamental of Redux
Integration of Highcharts with React ( JavaScript library )
Serialization & De-serialization in Java
How to Make Each Round of Testing Count?
Model View Presenter For Android
Retrofit Library In Android

Recently uploaded (20)

PDF
MIND Revenue Release Quarter 2 2025 Press Release
PPTX
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
PDF
Review of recent advances in non-invasive hemoglobin estimation
PDF
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
PPTX
Machine Learning_overview_presentation.pptx
PDF
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
PPT
Teaching material agriculture food technology
PDF
Spectral efficient network and resource selection model in 5G networks
PDF
The Rise and Fall of 3GPP – Time for a Sabbatical?
PPTX
MYSQL Presentation for SQL database connectivity
PDF
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
PPT
“AI and Expert System Decision Support & Business Intelligence Systems”
PDF
Electronic commerce courselecture one. Pdf
PDF
Agricultural_Statistics_at_a_Glance_2022_0.pdf
PPTX
Digital-Transformation-Roadmap-for-Companies.pptx
PDF
Encapsulation_ Review paper, used for researhc scholars
PDF
Approach and Philosophy of On baking technology
PDF
Encapsulation theory and applications.pdf
PDF
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
PDF
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
MIND Revenue Release Quarter 2 2025 Press Release
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
Review of recent advances in non-invasive hemoglobin estimation
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
Machine Learning_overview_presentation.pptx
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
Teaching material agriculture food technology
Spectral efficient network and resource selection model in 5G networks
The Rise and Fall of 3GPP – Time for a Sabbatical?
MYSQL Presentation for SQL database connectivity
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
“AI and Expert System Decision Support & Business Intelligence Systems”
Electronic commerce courselecture one. Pdf
Agricultural_Statistics_at_a_Glance_2022_0.pdf
Digital-Transformation-Roadmap-for-Companies.pptx
Encapsulation_ Review paper, used for researhc scholars
Approach and Philosophy of On baking technology
Encapsulation theory and applications.pdf
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...

Concept of Stream API Java 1.8

  • 1. Concept of Stream API Java 1.8 Stream concept is introduced in java 1.8 and present in java.util.stream package.It is used to process the object from collection or any group of objects or data source. We can easily understand java stream concept by it's name stream, In stream water flows from one water source to destination and we can perform some operations like filtering, collecting etc on stream to get useful water,Same in java stream concept we can think object flows from one object source to destination by Stream pipelines (A stream pipeline is composed of a stream source, zero or more intermediate operations, and a terminal operation.) We should be familiar with some concept before going to further in stream concept and remember one thing streams don’t actually store elements; they are computed on demand. 1> C.stream() - we are going to get an stream object where C is collection object. 2>filter()- used to do filtering based on predicate(predicate is nothing but boolean condition, basically it is a functional interface so it can be replaced by lambda expression) 3> collect()- it is a terminal operation use to collect filtered or mapped data. Example- ArrayList<Integer> arrayList = new ArrayList<Integer>(); arrayList.add(0); arrayList.add(10); arrayList.add(20); arrayList.add(5); arrayList.add(15); arrayList.add(25); System.out.println(arrayList);
  • 2. output will be [0,10,20,5,15,25]. Now,we want to get a new ArrayList of integer which contains only even integers from existing arrayList object. Here, how we process collection object without stream concept List<Integer> list = new ArrayList<Integer>(); for(Integer i:arrayList){ if(i%2 == 0) list.add(i); } System.out.println(arrayList); output will be [0,10,20] with stream concept- List<Integer> list = arrayList.stream().filter(i->i%2==0).collect(Collectors.toList()); System.out.println(arrayList); output will be [0,10,20].In collection if we perform bulk operation then highly recommended to use stream concept. some more useful method of stream concept- 4> map()- for every object if we want to perform some action and want some result object then we use map method if we want to increase all the arrayList data by 5 then we can use this function. List<Integer> l = arrayList.stream().map(i-> i+5).collect(Collectors.toList()); it always take functional interface(interface which contains only one method) so we can replace it by lambda expression. 5> count() - use to count how many objects are there in stream ( not in data source). long noOfEvenInteger = arrayList.stream().filter(i-> i%2!=0).count(); 6> sorted() - use to perform sorting in ascending order. List<Integer> l = arrayList.stream().sorted().collect(Collectors.toList()); return an ascending order list. for customization we go for comparator concept.comparator is also a functional interface so we can replace it by lambda expression.
  • 3. Comparator - it contains compare() method and we can replace it by lambda expression compare(obj1, obj2)- return -ve if obj1 has to come before obj2; return +ve if obj1 has to come after obj2; return 0 if both are equal; we perform sorting in desc order here, what we do inside compare method (i1, i2)-> (i1<i2)?1:(i1 > i2)?-1:0 . We compare two object i1 and i2 if i1 is smaller than i2 it means i1 has to come before i2, so it will return +ve else it check i1 is bigger or not if yes it means i1 has to come after i2, so it will return -ve and if both are equal then it will return 0; List<Integer> list = arrayList.stream().sorted((i1,i2)->(i1, i2)-> (i1<i2)?1:(i1 > i2)?-1:0).collect(Collectors.toList()); 7> forEach() - for every element if we want to perform some functionality. arrayList.stream().forEach(System.out::println); it will print all object.We can call our own method also. Consumer<Integer> fun = i->{System.out.println("square of"+i+"is = "+(i*i))}; arrayList.stream().forEach(fun); 8> toArray() - to convert stream of object into array. Integer[] arr = arrayList.stream().toArray(Integer::new); 9> Stream.of(arr) - use to get stream from array or other group of data; Stream s = stream.of(10,1,2,12,34); s.forEach(System.out::println);