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
vishal choudhary
 
PDF
Java ArrayList Tutorial | Edureka
Edureka!
 
PPT
Engineering lecture ppt by venay magen
venaymagen19
 
PPTX
F# array searching
DrRajeshreeKhande
 
PDF
Priorty queue
sidra ali
 
PDF
Stacks,queues,linked-list
pinakspatel
 
PPT
Array Presentation (EngineerBaBu.com)
EngineerBabu
 
PDF
Przywitaj się z reactive extensions
Marcin Juraszek
 
Array list
vishal choudhary
 
Java ArrayList Tutorial | Edureka
Edureka!
 
Engineering lecture ppt by venay magen
venaymagen19
 
F# array searching
DrRajeshreeKhande
 
Priorty queue
sidra ali
 
Stacks,queues,linked-list
pinakspatel
 
Array Presentation (EngineerBaBu.com)
EngineerBabu
 
Przywitaj się z reactive extensions
Marcin Juraszek
 

What's hot (20)

PPTX
Net (f#) array
DrRajeshreeKhande
 
PDF
Lecture 6 - Arrays
Syed Afaq Shah MACS CP
 
PDF
Python programming : Arrays
Emertxe Information Technologies Pvt Ltd
 
PDF
The Ring programming language version 1.5.2 book - Part 21 of 181
Mahmoud Samir Fayed
 
PDF
Arrays In Python | Python Array Operations | Edureka
Edureka!
 
PPTX
Unit 5 linked list
Dabbal Singh Mahara
 
PDF
The Functional Programming Triad of fold, scan and iterate
Philip Schwarz
 
PDF
The Functional Programming Triad of fold, scan and iterate
Philip Schwarz
 
PPTX
Python array
Arnab Chakraborty
 
PDF
Arrays in python
Lifna C.S
 
PPTX
Min priority queue
9854098540
 
PPTX
Max priority queue
9854098540
 
PDF
Java8
Sunil Kumar
 
ODP
Knolx session
Knoldus Inc.
 
PPTX
Address calculation-sort
Vasim Pathan
 
PPTX
Data Structures - Lecture 3 [Arrays]
Muhammad Hammad Waseem
 
DOC
Array properties
Shravan Sharma
 
PPT
Unit 3 stack
kalyanineve
 
PPT
Java Presentation
mdfkhan625
 
Net (f#) array
DrRajeshreeKhande
 
Lecture 6 - Arrays
Syed Afaq Shah MACS CP
 
Python programming : Arrays
Emertxe Information Technologies Pvt Ltd
 
The Ring programming language version 1.5.2 book - Part 21 of 181
Mahmoud Samir Fayed
 
Arrays In Python | Python Array Operations | Edureka
Edureka!
 
Unit 5 linked list
Dabbal Singh Mahara
 
The Functional Programming Triad of fold, scan and iterate
Philip Schwarz
 
The Functional Programming Triad of fold, scan and iterate
Philip Schwarz
 
Python array
Arnab Chakraborty
 
Arrays in python
Lifna C.S
 
Min priority queue
9854098540
 
Max priority queue
9854098540
 
Knolx session
Knoldus Inc.
 
Address calculation-sort
Vasim Pathan
 
Data Structures - Lecture 3 [Arrays]
Muhammad Hammad Waseem
 
Array properties
Shravan Sharma
 
Unit 3 stack
kalyanineve
 
Java Presentation
mdfkhan625
 
Ad

Similar to Concept of Stream API Java 1.8 (20)

PPT
Collection Framework.power point presentation.......
Betty333100
 
PPT
Java10 Collections and Information
SoftNutx
 
DOCX
ArrayList.docx
veerendranath12
 
PDF
Reactive programming with RxJava
Jobaer Chowdhury
 
PDF
Aj unit2 notesjavadatastructures
Arthik Daniel
 
PPTX
Xebicon2013 scala vsjava_final
Urs Peter
 
PPTX
collectionframework-141116005344-conversion-gate01.pptx
hemanth248901
 
PPTX
Nature Activities Binder _ by Slidesgo.pptx
IllllBikkySharmaIlll
 
PPTX
Collections
sagsharma
 
PPT
Collections Framework
Sunil OS
 
PDF
07 java collection
Abhishek Khune
 
PDF
There are a couple of new methods that you will be writing for this pr.pdf
aamousnowov
 
PPT
Array 31.8.2020 updated
vrgokila
 
PDF
Collections Api - Java
Drishti Bhalla
 
PPT
List in java
nitin kumar
 
PPTX
Java Foundations: Maps, Lambda and Stream API
Svetlin Nakov
 
PPT
collections
javeed_mhd
 
DOCX
Java collections notes
Surendar Meesala
 
Collection Framework.power point presentation.......
Betty333100
 
Java10 Collections and Information
SoftNutx
 
ArrayList.docx
veerendranath12
 
Reactive programming with RxJava
Jobaer Chowdhury
 
Aj unit2 notesjavadatastructures
Arthik Daniel
 
Xebicon2013 scala vsjava_final
Urs Peter
 
collectionframework-141116005344-conversion-gate01.pptx
hemanth248901
 
Nature Activities Binder _ by Slidesgo.pptx
IllllBikkySharmaIlll
 
Collections
sagsharma
 
Collections Framework
Sunil OS
 
07 java collection
Abhishek Khune
 
There are a couple of new methods that you will be writing for this pr.pdf
aamousnowov
 
Array 31.8.2020 updated
vrgokila
 
Collections Api - Java
Drishti Bhalla
 
List in java
nitin kumar
 
Java Foundations: Maps, Lambda and Stream API
Svetlin Nakov
 
collections
javeed_mhd
 
Java collections notes
Surendar Meesala
 
Ad

More from InnovationM (20)

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

Recently uploaded (20)

PDF
Best ERP System for Manufacturing in India | Elite Mindz
Elite Mindz
 
PPTX
How to Build a Scalable Micro-Investing Platform in 2025 - A Founder’s Guide ...
Third Rock Techkno
 
PDF
Cloud-Migration-Best-Practices-A-Practical-Guide-to-AWS-Azure-and-Google-Clou...
Artjoker Software Development Company
 
PDF
CIFDAQ'S Market Insight: BTC to ETH money in motion
CIFDAQ
 
PDF
Event Presentation Google Cloud Next Extended 2025
minhtrietgect
 
PPTX
New ThousandEyes Product Innovations: Cisco Live June 2025
ThousandEyes
 
PPTX
AI and Robotics for Human Well-being.pptx
JAYMIN SUTHAR
 
PPTX
ChatGPT's Deck on The Enduring Legacy of Fax Machines
Greg Swan
 
PDF
Tea4chat - another LLM Project by Kerem Atam
a0m0rajab1
 
PDF
The Evolution of KM Roles (Presented at Knowledge Summit Dublin 2025)
Enterprise Knowledge
 
PPTX
Comunidade Salesforce São Paulo - Desmistificando o Omnistudio (Vlocity)
Francisco Vieira Júnior
 
PDF
Security features in Dell, HP, and Lenovo PC systems: A research-based compar...
Principled Technologies
 
PDF
This slide provides an overview Technology
mineshkharadi333
 
PDF
Accelerating Oracle Database 23ai Troubleshooting with Oracle AHF Fleet Insig...
Sandesh Rao
 
PDF
REPORT: Heating appliances market in Poland 2024
SPIUG
 
PDF
SparkLabs Primer on Artificial Intelligence 2025
SparkLabs Group
 
PDF
BLW VOCATIONAL TRAINING SUMMER INTERNSHIP REPORT
codernjn73
 
PDF
Structs to JSON: How Go Powers REST APIs
Emily Achieng
 
PDF
Data_Analytics_vs_Data_Science_vs_BI_by_CA_Suvidha_Chaplot.pdf
CA Suvidha Chaplot
 
PDF
How Open Source Changed My Career by abdelrahman ismail
a0m0rajab1
 
Best ERP System for Manufacturing in India | Elite Mindz
Elite Mindz
 
How to Build a Scalable Micro-Investing Platform in 2025 - A Founder’s Guide ...
Third Rock Techkno
 
Cloud-Migration-Best-Practices-A-Practical-Guide-to-AWS-Azure-and-Google-Clou...
Artjoker Software Development Company
 
CIFDAQ'S Market Insight: BTC to ETH money in motion
CIFDAQ
 
Event Presentation Google Cloud Next Extended 2025
minhtrietgect
 
New ThousandEyes Product Innovations: Cisco Live June 2025
ThousandEyes
 
AI and Robotics for Human Well-being.pptx
JAYMIN SUTHAR
 
ChatGPT's Deck on The Enduring Legacy of Fax Machines
Greg Swan
 
Tea4chat - another LLM Project by Kerem Atam
a0m0rajab1
 
The Evolution of KM Roles (Presented at Knowledge Summit Dublin 2025)
Enterprise Knowledge
 
Comunidade Salesforce São Paulo - Desmistificando o Omnistudio (Vlocity)
Francisco Vieira Júnior
 
Security features in Dell, HP, and Lenovo PC systems: A research-based compar...
Principled Technologies
 
This slide provides an overview Technology
mineshkharadi333
 
Accelerating Oracle Database 23ai Troubleshooting with Oracle AHF Fleet Insig...
Sandesh Rao
 
REPORT: Heating appliances market in Poland 2024
SPIUG
 
SparkLabs Primer on Artificial Intelligence 2025
SparkLabs Group
 
BLW VOCATIONAL TRAINING SUMMER INTERNSHIP REPORT
codernjn73
 
Structs to JSON: How Go Powers REST APIs
Emily Achieng
 
Data_Analytics_vs_Data_Science_vs_BI_by_CA_Suvidha_Chaplot.pdf
CA Suvidha Chaplot
 
How Open Source Changed My Career by abdelrahman ismail
a0m0rajab1
 

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);