SlideShare a Scribd company logo
Introduction to
Java 8 Stream
API
Sidlyarevich Vladislav
Contacts/samples
● https://github.com/kuksenko/jdk8-lambda-samples
● https://github.com/vlsidlyarevich/Stream-API-Examples
● https://www.youtube.com/watch?v=O8oN4KSZEXE
● https://www.youtube.com/watch?v=i0Jr2l3jrDA
Java 8 language features
● Lambdas and Functional Interfaces
● Interface’s Default and Static Methods
● Method References
● Repeating annotations
Java 8 language features
● Date/Time API (JSR 310)
● Nashorn JavaScript engine
● Base64
● Parallel Arrays
● Concurrency
Java 8 language features
STREAM API!
Set<Person> freshBlood = new HashSet<>();
for (Person person : team) {
if (person.age <= 25) {
freshBlood.add(person);
}
}
List<Person> sortedFreshBlood = new ArrayList<>(freshBlood);
Collections.sort(sortedFreshBlood, new Comparator<Person>() {
@Override
public int compare(Person o1, Person o2) {
return Integer.compare(o1.age, o2.age);
}
});
for (Person person : sortedFreshBlood) {
System.out.println(person.name);
}
team.stream()
.filter(person -> person.age <= 25)
.collect(Collectors.toSet())
.stream()
.sorted(comparing(person -> person.age))
.forEach(person -> System.out.println(person.name));
source op op op
terminate
Profit
sources: collections, iterators, api’s
operations: filter, map, reduce, etc
sinks: collections, locals
What is Stream?
Multiplicity of values
Lazy
Single use
Not mutate the source
Ordered/Unordered
Parallel/Sequential
IntStream, DoubleStream, LongStream
What is Stream?
Sequence of elements − A stream provides a set of elements of specific type in a sequential
manner. A stream gets/computes elements on demand. It never stores the elements.
Source − Stream takes Collections, Arrays, or I/O resources as input source.
Aggregate operations − Stream supports aggregate operations like filter, map, limit, reduce,
find, match, and so on.
Pipelining − Most of the stream operations return stream itself so that their result can be
pipelined. These operations are called intermediate operations and their function is to take
input, process them, and return output to the target. collect() method is a terminal operation
which is normally present at the end of the pipelining operation to mark the end of the stream.
Automatic iterations − Stream operations do the iterations internally over the source elements
provided, in contrast to Collections where explicit iteration is required.
Stream pipeline
sources: team, stream operations: filter, sorted, forEach
sinks: collect
team.stream()
.filter(person -> person.age <= 25)
.collect(Collectors.toSet())
.stream()
.sorted(comparing(person -> person.age))
.forEach(person -> System.out.println(person.name));
Sources
Collections
Popular API’s (For example Regex)
String sentence = "Java 8 Stream tutorial";
Stream<String> regExpStream
= Pattern.compile("w").splitAsStream(sentence);
Sources
Infinite
Stream iterateStream = Stream.iterate(0, n -> n + 1).limit(2);
Function
Stream generatedStream = Stream.generate(Math::random).limit(5L);
List<String> arrList = new ArrayList<>();
Stream<String> arrListStream = arrList.stream(); //sized, ordered
List<String> linkedList = new LinkedList<>();
Stream<String> linkedListStream = linkedList.stream(); //sized, ordered
Set<String> hashSet = new HashSet<>();
Stream<String> hashSetStream = hashSet.stream(); //sized, distinct
Set<String> linkedHashSet = new LinkedHashSet<>();
Stream<String> linkedHashSetStream = linkedHashSet.stream(); //sized, distinct, ordered
Set<String> treeSet = new TreeSet<>();
Stream<String> treeSetStream = treeSet.stream(); //sized, distinct, sorted, ordered
source op op op
terminate
ProfitIntermediate
● Stream<S> s.distinct();
● Stream<S> s.filter(Predicate <S>);
● Stream<T> s.map(Function<S, T>);
● Stream<T> s.flatMap(Function<S, Stream<T>>);
● Stream<S> s.peek(Consumer<S>)
● Stream<S> s.sorted()
● Stream<S> s.limit(long);
● Stream<S> s.skip(long);
● Stream<S> s.distinct();
● Stream<S> s.filter(Predicate <S>);
● Stream<T> s.map(Function<S, T>);
● Stream<T> s.map(Function<S, Stream<T>>);
● Stream<S> s.peek(Consumer<S>)
● Stream<S> s.sorted()
● Stream<S> s.limit(long);
● Stream<S> s.skip(long);
● Stream<S> s.unordered();
● Stream<S> s.parallel();
● Stream<S> s.sequential();
Terminal operations = Profit
Terminal operations
iteration: forEach, iterator
search: findFirst, findAny
check: allMatch, anyMatch, noneMatch
aggregation: reduction, collectors
Short-circuiting
All find*
All match*
limit
int number = Stream.iterate(1, n -> n * 2)
.filter(n -> n % 1024 == 0)
.findFirst().get();
Examples
List<Transaction> groceryTransactions = new Arraylist<>();
for(Transaction t: transactions) {
if(t.getType() == Transaction.GROCERY) {
groceryTransactions.add(t);
}
}
Collections.sort(groceryTransactions, new Comparator() {
public int compare(Transaction t1, Transaction t2) {
return t2.getValue().compareTo(t1.getValue());
}
});
List<Integer> transactionIds = new ArrayList<>();
for(Transaction t: groceryTransactions) {
transactionsIds.add(t.getId());
}
Examples
List<Transaction> groceryTransactions = new Arraylist<>();
transactions.Stream()
.filter(t.getType() == Transaction.GROCERY)
Examples
List<Transaction> groceryTransactions = new Arraylist<>();
transactions.Stream()
.filter(t.getType() == Transaction.GROCERY)
.sorted(comparing(t -> t.getValue)
Examples
List<Transaction> groceryTransactions = new Arraylist<>();
transactions.Stream()
.filter(t.getType() == Transaction.GROCERY)
.sorted(comparing(t -> t.getValue).reversed)
Examples
List<Transaction> groceryTransactions = new Arraylist<>();
transactions.Stream()
.filter(t.getType() == Transaction.GROCERY)
.sorted(comparing(t -> t.getValue).reversed)
.map(transaction -> transaction.getId())
Examples
List<Transaction> groceryTransactions = new Arraylist<>();
transactions.Stream()
.filter(t.getType() == Transaction.GROCERY)
.sorted(comparing(t -> t.getValue).reversed)
.map(transaction -> transaction.getId())
.collect(Collectors.toList())
Examples
List<Transaction> groceryTransactions = new Arraylist<>();
transactions.Stream()
.filter(t.getType() == Transaction.GROCERY)
.sorted(comparing(Transaction::getValue).reversed)
.map(Transaction::getId())
.collect(Collectors.toList())
Thanks for your attention!

More Related Content

PPTX
Java 8 Lambda and Streams
Venkata Naga Ravi
 
PDF
Java 8 Lambda Expressions
Scott Leberknight
 
PDF
Spring Framework - Core
Dzmitry Naskou
 
PPTX
Java 8 presentation
Van Huong
 
PPTX
Java 8 streams
Manav Prasad
 
PDF
Spring Boot
Pei-Tang Huang
 
PDF
Java 8 Lambda Built-in Functional Interfaces
Ganesh Samarthyam
 
PPTX
Java 8 lambda
Manav Prasad
 
Java 8 Lambda and Streams
Venkata Naga Ravi
 
Java 8 Lambda Expressions
Scott Leberknight
 
Spring Framework - Core
Dzmitry Naskou
 
Java 8 presentation
Van Huong
 
Java 8 streams
Manav Prasad
 
Spring Boot
Pei-Tang Huang
 
Java 8 Lambda Built-in Functional Interfaces
Ganesh Samarthyam
 
Java 8 lambda
Manav Prasad
 

What's hot (20)

PPTX
java 8 new features
Rohit Verma
 
PPTX
Spring boot Introduction
Jeevesh Pandey
 
PPTX
Java 8 - Features Overview
Sergii Stets
 
PPTX
Spring & hibernate
Santosh Kumar Kar
 
PDF
Lambda Expressions in Java
Erhan Bagdemir
 
PDF
Introduction to Redux
Ignacio Martín
 
PDF
Java 8 Stream API. A different way to process collections.
David Gómez García
 
PPTX
Spring Boot and REST API
07.pallav
 
PDF
Streams in Java 8
Tobias Coetzee
 
PPTX
Spring data jpa
Jeevesh Pandey
 
ODP
Introduction to Java 8
Knoldus Inc.
 
PDF
Java 8 features
NexThoughts Technologies
 
PDF
Spring Boot
HongSeong Jeon
 
PPSX
Strings in Java
Hitesh-Java
 
PDF
Java 8 Lambda Expressions & Streams
NewCircle Training
 
PDF
Java 8 lambda expressions
Logan Chien
 
PPT
Java collections concept
kumar gaurav
 
PPTX
Angular Data Binding
Jennifer Estrada
 
PPTX
[Final] ReactJS presentation
洪 鹏发
 
PPT
Java 8 Streams
Manvendra Singh
 
java 8 new features
Rohit Verma
 
Spring boot Introduction
Jeevesh Pandey
 
Java 8 - Features Overview
Sergii Stets
 
Spring & hibernate
Santosh Kumar Kar
 
Lambda Expressions in Java
Erhan Bagdemir
 
Introduction to Redux
Ignacio Martín
 
Java 8 Stream API. A different way to process collections.
David Gómez García
 
Spring Boot and REST API
07.pallav
 
Streams in Java 8
Tobias Coetzee
 
Spring data jpa
Jeevesh Pandey
 
Introduction to Java 8
Knoldus Inc.
 
Java 8 features
NexThoughts Technologies
 
Spring Boot
HongSeong Jeon
 
Strings in Java
Hitesh-Java
 
Java 8 Lambda Expressions & Streams
NewCircle Training
 
Java 8 lambda expressions
Logan Chien
 
Java collections concept
kumar gaurav
 
Angular Data Binding
Jennifer Estrada
 
[Final] ReactJS presentation
洪 鹏发
 
Java 8 Streams
Manvendra Singh
 
Ad

Similar to Introduction to java 8 stream api (20)

PPTX
New Features in JDK 8
Martin Toshev
 
PPTX
Hot Streaming Java
nick_maiorano
 
PPTX
What is new in Java 8
Sandeep Kr. Singh
 
PPTX
Tk2323 lecture 9 api json
MengChun Lam
 
PDF
Functional programming in Java 8 - workshop at flatMap Oslo 2014
Fredrik Vraalsen
 
PPTX
New features in jdk8 iti
Ahmed mar3y
 
PDF
Java 8 - Return of the Java
Fredrik Vraalsen
 
PDF
Xlab #1: Advantages of functional programming in Java 8
XSolve
 
PPTX
FUNctional Programming in Java 8
Richard Walker
 
PDF
Ruby on Rails Oracle adaptera izstrāde
Raimonds Simanovskis
 
PDF
Java 8 new features or the ones you might actually use
Sharon Rozinsky
 
PDF
Java 8 Stream API (Valdas Zigas)
Kaunas Java User Group
 
PPTX
Belfast JUG 23-10-2013
eamonnlong
 
PDF
Node.js Stream API
The Software House
 
PDF
Specification-Driven Development of REST APIs by Alexander Zinchuk
OdessaJS Conf
 
PPTX
Java 8 Intro - Core Features
GlobalLogic Ukraine
 
PDF
Intro to Spark and Spark SQL
jeykottalam
 
PPT
Developing RESTful WebServices using Jersey
b_kathir
 
PDF
Eclipse Collections, Java Streams & Vavr - What's in them for Functional Pro...
Naresha K
 
PDF
Using Ruby on Rails with legacy Oracle databases
Raimonds Simanovskis
 
New Features in JDK 8
Martin Toshev
 
Hot Streaming Java
nick_maiorano
 
What is new in Java 8
Sandeep Kr. Singh
 
Tk2323 lecture 9 api json
MengChun Lam
 
Functional programming in Java 8 - workshop at flatMap Oslo 2014
Fredrik Vraalsen
 
New features in jdk8 iti
Ahmed mar3y
 
Java 8 - Return of the Java
Fredrik Vraalsen
 
Xlab #1: Advantages of functional programming in Java 8
XSolve
 
FUNctional Programming in Java 8
Richard Walker
 
Ruby on Rails Oracle adaptera izstrāde
Raimonds Simanovskis
 
Java 8 new features or the ones you might actually use
Sharon Rozinsky
 
Java 8 Stream API (Valdas Zigas)
Kaunas Java User Group
 
Belfast JUG 23-10-2013
eamonnlong
 
Node.js Stream API
The Software House
 
Specification-Driven Development of REST APIs by Alexander Zinchuk
OdessaJS Conf
 
Java 8 Intro - Core Features
GlobalLogic Ukraine
 
Intro to Spark and Spark SQL
jeykottalam
 
Developing RESTful WebServices using Jersey
b_kathir
 
Eclipse Collections, Java Streams & Vavr - What's in them for Functional Pro...
Naresha K
 
Using Ruby on Rails with legacy Oracle databases
Raimonds Simanovskis
 
Ad

Recently uploaded (20)

PDF
Make GenAI investments go further with the Dell AI Factory - Infographic
Principled Technologies
 
PDF
A Day in the Life of Location Data - Turning Where into How.pdf
Precisely
 
PDF
BLW VOCATIONAL TRAINING SUMMER INTERNSHIP REPORT
codernjn73
 
PDF
Why Your AI & Cybersecurity Hiring Still Misses the Mark in 2025
Virtual Employee Pvt. Ltd.
 
PDF
Software Development Methodologies in 2025
KodekX
 
PDF
Revolutionize Operations with Intelligent IoT Monitoring and Control
Rejig Digital
 
PDF
Advances in Ultra High Voltage (UHV) Transmission and Distribution Systems.pdf
Nabajyoti Banik
 
PPTX
C Programming Basics concept krnppt.pptx
Karan Prajapat
 
PDF
DevOps & Developer Experience Summer BBQ
AUGNYC
 
PPTX
Comunidade Salesforce São Paulo - Desmistificando o Omnistudio (Vlocity)
Francisco Vieira Júnior
 
PDF
Accelerating Oracle Database 23ai Troubleshooting with Oracle AHF Fleet Insig...
Sandesh Rao
 
PDF
Google’s NotebookLM Unveils Video Overviews
SOFTTECHHUB
 
PDF
The Evolution of KM Roles (Presented at Knowledge Summit Dublin 2025)
Enterprise Knowledge
 
PDF
This slide provides an overview Technology
mineshkharadi333
 
PPTX
cloud computing vai.pptx for the project
vaibhavdobariyal79
 
PDF
Building High-Performance Oracle Teams: Strategic Staffing for Database Manag...
SMACT Works
 
PDF
Software Development Company | KodekX
KodekX
 
PDF
Oracle AI Vector Search- Getting Started and what's new in 2025- AIOUG Yatra ...
Sandesh Rao
 
PDF
Presentation about Hardware and Software in Computer
snehamodhawadiya
 
PPTX
AI and Robotics for Human Well-being.pptx
JAYMIN SUTHAR
 
Make GenAI investments go further with the Dell AI Factory - Infographic
Principled Technologies
 
A Day in the Life of Location Data - Turning Where into How.pdf
Precisely
 
BLW VOCATIONAL TRAINING SUMMER INTERNSHIP REPORT
codernjn73
 
Why Your AI & Cybersecurity Hiring Still Misses the Mark in 2025
Virtual Employee Pvt. Ltd.
 
Software Development Methodologies in 2025
KodekX
 
Revolutionize Operations with Intelligent IoT Monitoring and Control
Rejig Digital
 
Advances in Ultra High Voltage (UHV) Transmission and Distribution Systems.pdf
Nabajyoti Banik
 
C Programming Basics concept krnppt.pptx
Karan Prajapat
 
DevOps & Developer Experience Summer BBQ
AUGNYC
 
Comunidade Salesforce São Paulo - Desmistificando o Omnistudio (Vlocity)
Francisco Vieira Júnior
 
Accelerating Oracle Database 23ai Troubleshooting with Oracle AHF Fleet Insig...
Sandesh Rao
 
Google’s NotebookLM Unveils Video Overviews
SOFTTECHHUB
 
The Evolution of KM Roles (Presented at Knowledge Summit Dublin 2025)
Enterprise Knowledge
 
This slide provides an overview Technology
mineshkharadi333
 
cloud computing vai.pptx for the project
vaibhavdobariyal79
 
Building High-Performance Oracle Teams: Strategic Staffing for Database Manag...
SMACT Works
 
Software Development Company | KodekX
KodekX
 
Oracle AI Vector Search- Getting Started and what's new in 2025- AIOUG Yatra ...
Sandesh Rao
 
Presentation about Hardware and Software in Computer
snehamodhawadiya
 
AI and Robotics for Human Well-being.pptx
JAYMIN SUTHAR
 

Introduction to java 8 stream api

  • 1. Introduction to Java 8 Stream API Sidlyarevich Vladislav
  • 2. Contacts/samples ● https://github.com/kuksenko/jdk8-lambda-samples ● https://github.com/vlsidlyarevich/Stream-API-Examples ● https://www.youtube.com/watch?v=O8oN4KSZEXE ● https://www.youtube.com/watch?v=i0Jr2l3jrDA
  • 3. Java 8 language features ● Lambdas and Functional Interfaces ● Interface’s Default and Static Methods ● Method References ● Repeating annotations
  • 4. Java 8 language features ● Date/Time API (JSR 310) ● Nashorn JavaScript engine ● Base64 ● Parallel Arrays ● Concurrency
  • 5. Java 8 language features STREAM API!
  • 6. Set<Person> freshBlood = new HashSet<>(); for (Person person : team) { if (person.age <= 25) { freshBlood.add(person); } } List<Person> sortedFreshBlood = new ArrayList<>(freshBlood); Collections.sort(sortedFreshBlood, new Comparator<Person>() { @Override public int compare(Person o1, Person o2) { return Integer.compare(o1.age, o2.age); } }); for (Person person : sortedFreshBlood) { System.out.println(person.name); }
  • 7. team.stream() .filter(person -> person.age <= 25) .collect(Collectors.toSet()) .stream() .sorted(comparing(person -> person.age)) .forEach(person -> System.out.println(person.name));
  • 8. source op op op terminate Profit
  • 9. sources: collections, iterators, api’s operations: filter, map, reduce, etc sinks: collections, locals
  • 10. What is Stream? Multiplicity of values Lazy Single use Not mutate the source Ordered/Unordered Parallel/Sequential IntStream, DoubleStream, LongStream
  • 11. What is Stream? Sequence of elements − A stream provides a set of elements of specific type in a sequential manner. A stream gets/computes elements on demand. It never stores the elements. Source − Stream takes Collections, Arrays, or I/O resources as input source. Aggregate operations − Stream supports aggregate operations like filter, map, limit, reduce, find, match, and so on. Pipelining − Most of the stream operations return stream itself so that their result can be pipelined. These operations are called intermediate operations and their function is to take input, process them, and return output to the target. collect() method is a terminal operation which is normally present at the end of the pipelining operation to mark the end of the stream. Automatic iterations − Stream operations do the iterations internally over the source elements provided, in contrast to Collections where explicit iteration is required.
  • 12. Stream pipeline sources: team, stream operations: filter, sorted, forEach sinks: collect team.stream() .filter(person -> person.age <= 25) .collect(Collectors.toSet()) .stream() .sorted(comparing(person -> person.age)) .forEach(person -> System.out.println(person.name));
  • 13. Sources Collections Popular API’s (For example Regex) String sentence = "Java 8 Stream tutorial"; Stream<String> regExpStream = Pattern.compile("w").splitAsStream(sentence);
  • 14. Sources Infinite Stream iterateStream = Stream.iterate(0, n -> n + 1).limit(2); Function Stream generatedStream = Stream.generate(Math::random).limit(5L);
  • 15. List<String> arrList = new ArrayList<>(); Stream<String> arrListStream = arrList.stream(); //sized, ordered List<String> linkedList = new LinkedList<>(); Stream<String> linkedListStream = linkedList.stream(); //sized, ordered Set<String> hashSet = new HashSet<>(); Stream<String> hashSetStream = hashSet.stream(); //sized, distinct Set<String> linkedHashSet = new LinkedHashSet<>(); Stream<String> linkedHashSetStream = linkedHashSet.stream(); //sized, distinct, ordered Set<String> treeSet = new TreeSet<>(); Stream<String> treeSetStream = treeSet.stream(); //sized, distinct, sorted, ordered
  • 16. source op op op terminate ProfitIntermediate
  • 17. ● Stream<S> s.distinct(); ● Stream<S> s.filter(Predicate <S>); ● Stream<T> s.map(Function<S, T>); ● Stream<T> s.flatMap(Function<S, Stream<T>>); ● Stream<S> s.peek(Consumer<S>) ● Stream<S> s.sorted() ● Stream<S> s.limit(long); ● Stream<S> s.skip(long);
  • 18. ● Stream<S> s.distinct(); ● Stream<S> s.filter(Predicate <S>); ● Stream<T> s.map(Function<S, T>); ● Stream<T> s.map(Function<S, Stream<T>>); ● Stream<S> s.peek(Consumer<S>) ● Stream<S> s.sorted() ● Stream<S> s.limit(long); ● Stream<S> s.skip(long); ● Stream<S> s.unordered(); ● Stream<S> s.parallel(); ● Stream<S> s.sequential();
  • 20. Terminal operations iteration: forEach, iterator search: findFirst, findAny check: allMatch, anyMatch, noneMatch aggregation: reduction, collectors
  • 21. Short-circuiting All find* All match* limit int number = Stream.iterate(1, n -> n * 2) .filter(n -> n % 1024 == 0) .findFirst().get();
  • 22. Examples List<Transaction> groceryTransactions = new Arraylist<>(); for(Transaction t: transactions) { if(t.getType() == Transaction.GROCERY) { groceryTransactions.add(t); } } Collections.sort(groceryTransactions, new Comparator() { public int compare(Transaction t1, Transaction t2) { return t2.getValue().compareTo(t1.getValue()); } }); List<Integer> transactionIds = new ArrayList<>(); for(Transaction t: groceryTransactions) { transactionsIds.add(t.getId()); }
  • 23. Examples List<Transaction> groceryTransactions = new Arraylist<>(); transactions.Stream() .filter(t.getType() == Transaction.GROCERY)
  • 24. Examples List<Transaction> groceryTransactions = new Arraylist<>(); transactions.Stream() .filter(t.getType() == Transaction.GROCERY) .sorted(comparing(t -> t.getValue)
  • 25. Examples List<Transaction> groceryTransactions = new Arraylist<>(); transactions.Stream() .filter(t.getType() == Transaction.GROCERY) .sorted(comparing(t -> t.getValue).reversed)
  • 26. Examples List<Transaction> groceryTransactions = new Arraylist<>(); transactions.Stream() .filter(t.getType() == Transaction.GROCERY) .sorted(comparing(t -> t.getValue).reversed) .map(transaction -> transaction.getId())
  • 27. Examples List<Transaction> groceryTransactions = new Arraylist<>(); transactions.Stream() .filter(t.getType() == Transaction.GROCERY) .sorted(comparing(t -> t.getValue).reversed) .map(transaction -> transaction.getId()) .collect(Collectors.toList())
  • 28. Examples List<Transaction> groceryTransactions = new Arraylist<>(); transactions.Stream() .filter(t.getType() == Transaction.GROCERY) .sorted(comparing(Transaction::getValue).reversed) .map(Transaction::getId()) .collect(Collectors.toList())
  • 29. Thanks for your attention!