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

PPT
Java 8 Streams
PDF
Java 8 Stream API. A different way to process collections.
PDF
Java 8 Lambda Expressions & Streams
PPTX
Java 8 lambda
PPT
Spring Core
PDF
Spring boot
PDF
Lambda Expressions in Java
PPT
Major Java 8 features
Java 8 Streams
Java 8 Stream API. A different way to process collections.
Java 8 Lambda Expressions & Streams
Java 8 lambda
Spring Core
Spring boot
Lambda Expressions in Java
Major Java 8 features

What's hot (20)

PDF
Java 8 Lambda Expressions
PPT
JAVA OOP
PPTX
Dependency injection - the right way
PPTX
Java 8 streams
PDF
Java 8 Lambda Built-in Functional Interfaces
PDF
Java8 features
PPTX
Java 8 presentation
PPTX
Java 8 Lambda and Streams
PPSX
Spring - Part 1 - IoC, Di and Beans
PDF
Understanding react hooks
PDF
What is Dependency Injection in Spring Boot | Edureka
PDF
Angular Directives
PPTX
Spring boot Introduction
PPTX
Java Lambda Expressions.pptx
PPTX
Angular tutorial
PPTX
Lecture - 2 Environment setup & JDK, JRE, JVM
PDF
Spring Boot
PPTX
Spring Boot and REST API
PDF
Spring Framework - AOP
PDF
Java 8 Lambda Expressions
JAVA OOP
Dependency injection - the right way
Java 8 streams
Java 8 Lambda Built-in Functional Interfaces
Java8 features
Java 8 presentation
Java 8 Lambda and Streams
Spring - Part 1 - IoC, Di and Beans
Understanding react hooks
What is Dependency Injection in Spring Boot | Edureka
Angular Directives
Spring boot Introduction
Java Lambda Expressions.pptx
Angular tutorial
Lecture - 2 Environment setup & JDK, JRE, JVM
Spring Boot
Spring Boot and REST API
Spring Framework - AOP
Ad

Similar to Introduction to java 8 stream api (20)

PPTX
New Features in JDK 8
PPTX
Hot Streaming Java
PPTX
What is new in Java 8
PPTX
Tk2323 lecture 9 api json
PDF
Functional programming in Java 8 - workshop at flatMap Oslo 2014
PPTX
New features in jdk8 iti
PDF
Java 8 - Return of the Java
PDF
Xlab #1: Advantages of functional programming in Java 8
PPTX
FUNctional Programming in Java 8
PDF
Ruby on Rails Oracle adaptera izstrāde
PDF
Java 8 new features or the ones you might actually use
PDF
Java 8 Stream API (Valdas Zigas)
PPTX
Belfast JUG 23-10-2013
PDF
Node.js Stream API
PDF
Specification-Driven Development of REST APIs by Alexander Zinchuk
PPTX
Java 8 Intro - Core Features
PDF
Intro to Spark and Spark SQL
PPT
Developing RESTful WebServices using Jersey
PDF
Eclipse Collections, Java Streams & Vavr - What's in them for Functional Pro...
PDF
Using Ruby on Rails with legacy Oracle databases
New Features in JDK 8
Hot Streaming Java
What is new in Java 8
Tk2323 lecture 9 api json
Functional programming in Java 8 - workshop at flatMap Oslo 2014
New features in jdk8 iti
Java 8 - Return of the Java
Xlab #1: Advantages of functional programming in Java 8
FUNctional Programming in Java 8
Ruby on Rails Oracle adaptera izstrāde
Java 8 new features or the ones you might actually use
Java 8 Stream API (Valdas Zigas)
Belfast JUG 23-10-2013
Node.js Stream API
Specification-Driven Development of REST APIs by Alexander Zinchuk
Java 8 Intro - Core Features
Intro to Spark and Spark SQL
Developing RESTful WebServices using Jersey
Eclipse Collections, Java Streams & Vavr - What's in them for Functional Pro...
Using Ruby on Rails with legacy Oracle databases
Ad

Recently uploaded (20)

PDF
Advanced methodologies resolving dimensionality complications for autism neur...
PPTX
Machine Learning_overview_presentation.pptx
PDF
Dropbox Q2 2025 Financial Results & Investor Presentation
PDF
cuic standard and advanced reporting.pdf
PDF
gpt5_lecture_notes_comprehensive_20250812015547.pdf
PDF
Review of recent advances in non-invasive hemoglobin estimation
PDF
Unlocking AI with Model Context Protocol (MCP)
DOCX
The AUB Centre for AI in Media Proposal.docx
PDF
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
PDF
Encapsulation_ Review paper, used for researhc scholars
PPTX
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
PPTX
Digital-Transformation-Roadmap-for-Companies.pptx
PDF
Reach Out and Touch Someone: Haptics and Empathic Computing
PDF
Diabetes mellitus diagnosis method based random forest with bat algorithm
PPTX
20250228 LYD VKU AI Blended-Learning.pptx
PPTX
Programs and apps: productivity, graphics, security and other tools
PDF
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
PDF
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
PDF
Assigned Numbers - 2025 - Bluetooth® Document
PDF
NewMind AI Weekly Chronicles - August'25-Week II
Advanced methodologies resolving dimensionality complications for autism neur...
Machine Learning_overview_presentation.pptx
Dropbox Q2 2025 Financial Results & Investor Presentation
cuic standard and advanced reporting.pdf
gpt5_lecture_notes_comprehensive_20250812015547.pdf
Review of recent advances in non-invasive hemoglobin estimation
Unlocking AI with Model Context Protocol (MCP)
The AUB Centre for AI in Media Proposal.docx
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
Encapsulation_ Review paper, used for researhc scholars
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
Digital-Transformation-Roadmap-for-Companies.pptx
Reach Out and Touch Someone: Haptics and Empathic Computing
Diabetes mellitus diagnosis method based random forest with bat algorithm
20250228 LYD VKU AI Blended-Learning.pptx
Programs and apps: productivity, graphics, security and other tools
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
Assigned Numbers - 2025 - Bluetooth® Document
NewMind AI Weekly Chronicles - August'25-Week II

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!