Stream or Loop
Java 8 Stream API
Nikolay Petkov
Java 8 Goodies
●
Lambda expressions
aka functional interfaces
●
Method references
●
Default methods
Functional Interface
Lambda Expressions
Lambda Expressions
Method Reference
●
Static method
●
Instance method of particular instance
●
Instance method of particular type
●
Constructor
Static method reference
Instance method reference
Default Method
public interface Developer {
void writeCode();
default void writeTest() {
throw new ImPerfectException(
“Why bother? My code is perfect”);
}
}
External vs Internal Iteration
for (Car car : cars) {
if (“BMW”.equals(car.getBrand()) {
car.setOwner(“Alex”) ;
}
}
cars.forEach((car) -> {
if (“BMW”.equals(car.getBrand()) {
car.setOwner(“Alex”) ;
}
});
Stream API
Classes to support functional-style
operations on streams of elements, such as
map-reduce transformations on collections.
int oldestBmwAge = cars.stream()
.filter((car) -> “BMW”.equals(car.getBrand()))
.mapToInt((car) -> car.getAge())
.max();
Streams vs Collections
●
Not a storage
●
Functional by design
●
Possibly unbounded
●
Lazy by nature
●
Use and discard
Stream pipeline
A stream pipeline consists of:
●
a source (such as a Collection, an array, a
generator function, or an I/O channel);
●
followed by zero or more intermediate
operations such as Stream.filter or
Stream.map;
●
and a terminal operation such as
Stream.forEach or Stream.reduce.
Execution begins when the terminal operation
is invoked, and ends when the terminal
operation completes
Source
● Collection.stream()
● Arrays.stream(Object[])
● Stream.of(Object[])
● IntStream.range(int, int)
● BufferedReader.lines();
● Files.list(Path dir)
●
etc.
Intermediate - filter
Returns a stream consisting of the elements of this
stream that match the given predicate.
Stream<T> filter(Predicate<? super T> predicate)
filter((String s) -> s.lenght() < 20)
filter((String s) -> String::isEmpty))
Intermediate - sorted
Returns a stream consisting of the elements of this
stream, sorted according to the provided Comparator.
sorted(Comparator<? super T> comparator)
sorted(comparing(Person::getLastName)
sorted((s1, s2) -> s1.lenght() > s2.lenght())
Intermediate - map
Returns a stream consisting of the results of applying
the given function to the elements of this stream.
The map operation is kind of transformation of the
stream elements to another elements.
map(Function<T,R> mapper)
mapToInt((String s) -> s.lenght())
map((User u) -> User::getRole())
Terminal – max/min/sum/count
Returns the maximum/minimum element of this
stream according to the provided Comparator
max(Comparator<? super T> comparator)
max(comapring(Persone::getAge))
stream.mapToInt(Persone::getAge).sum()
A lot more follows
Questions?
Streams or Loops? Java 8 Stream API by Niki Petkov - Proxiad Bulgaria

More Related Content

PPTX
Introduction to java 8 stream api
PDF
Reactive programming on Android
PDF
Short intro to ECMAScript
PDF
Functional Principles for OO Developers
PDF
What You Need to Know about Lambdas
PPTX
Jug trojmiasto 2014.04.24 tricky stuff in java grammar and javac
PDF
Scalaz Stream: Rebirth
PPTX
Functional Reactive Programming (FRP): Working with RxJS
Introduction to java 8 stream api
Reactive programming on Android
Short intro to ECMAScript
Functional Principles for OO Developers
What You Need to Know about Lambdas
Jug trojmiasto 2014.04.24 tricky stuff in java grammar and javac
Scalaz Stream: Rebirth
Functional Reactive Programming (FRP): Working with RxJS

What's hot (20)

PDF
iOS Reactive Cocoa Pipeline
PDF
Scala for the web Lightning Talk
PDF
Functional Reactive Programming in Clojurescript
PDF
Contracts in-clojure-pete
ZIP
Lisp Macros in 20 Minutes (Featuring Clojure)
PPTX
Use of Apache Commons and Utilities
PDF
Productive Programming in Java 8 - with Lambdas and Streams
PDF
GDG Almaty Meetup: Reactive full-stack .NET web applications with WebSharper
PDF
LINQ Inside
PDF
(ThoughtWorks Away Day 2009) one or two things you may not know about typesys...
ODP
Lambda Chops - Recipes for Simpler, More Expressive Code
PDF
Modern Programming in Java 8 - Lambdas, Streams and Date Time API
ODP
Modern Java Features
PDF
JavaScript For CSharp Developer
PDF
Designing a Functional GraphQL Library
PPTX
Why TypeScript?
PDF
非同期javascriptの過去と未来
PDF
Scala coated JVM
PDF
Async Microservices with Twitter's Finagle
PDF
Maintainable JavaScript
iOS Reactive Cocoa Pipeline
Scala for the web Lightning Talk
Functional Reactive Programming in Clojurescript
Contracts in-clojure-pete
Lisp Macros in 20 Minutes (Featuring Clojure)
Use of Apache Commons and Utilities
Productive Programming in Java 8 - with Lambdas and Streams
GDG Almaty Meetup: Reactive full-stack .NET web applications with WebSharper
LINQ Inside
(ThoughtWorks Away Day 2009) one or two things you may not know about typesys...
Lambda Chops - Recipes for Simpler, More Expressive Code
Modern Programming in Java 8 - Lambdas, Streams and Date Time API
Modern Java Features
JavaScript For CSharp Developer
Designing a Functional GraphQL Library
Why TypeScript?
非同期javascriptの過去と未来
Scala coated JVM
Async Microservices with Twitter's Finagle
Maintainable JavaScript
Ad

Similar to Streams or Loops? Java 8 Stream API by Niki Petkov - Proxiad Bulgaria (20)

PPTX
Java 8 presentation
PDF
Charles Sharp: Java 8 Streams
PDF
Apouc 2014-java-8-create-the-future
PDF
Java Lambda internals with invoke dynamic
PDF
Lambda.pdf
PPTX
Java 8 Lambda and Streams
PPTX
PPTX
JDK8 Lambdas and Streams: Changing The Way You Think When Developing Java
PPTX
PPTX
Lambdas : Beyond The Basics
PPTX
Java8 training - Class 1
PPTX
Java 8
PPTX
java150929145120-lva1-app6892 (2).pptx
PPTX
A brief tour of modern Java
PDF
Streams in Java 8
ODP
Introduction to Java 8
PPTX
Java 8 lambda
PPTX
Java 8 Intro - Core Features
PPTX
Java 8 Features
PPTX
Lambdas and-streams-s ritter-v3
Java 8 presentation
Charles Sharp: Java 8 Streams
Apouc 2014-java-8-create-the-future
Java Lambda internals with invoke dynamic
Lambda.pdf
Java 8 Lambda and Streams
JDK8 Lambdas and Streams: Changing The Way You Think When Developing Java
Lambdas : Beyond The Basics
Java8 training - Class 1
Java 8
java150929145120-lva1-app6892 (2).pptx
A brief tour of modern Java
Streams in Java 8
Introduction to Java 8
Java 8 lambda
Java 8 Intro - Core Features
Java 8 Features
Lambdas and-streams-s ritter-v3
Ad

Recently uploaded (20)

PDF
احياء السادس العلمي - الفصل الثالث (التكاثر) منهج متميزين/كلية بغداد/موهوبين
PDF
1.3 FINAL REVISED K-10 PE and Health CG 2023 Grades 4-10 (1).pdf
PPTX
B.Sc. DS Unit 2 Software Engineering.pptx
DOC
Soft-furnishing-By-Architect-A.F.M.Mohiuddin-Akhand.doc
PDF
advance database management system book.pdf
PPTX
Virtual and Augmented Reality in Current Scenario
DOCX
Cambridge-Practice-Tests-for-IELTS-12.docx
PDF
ChatGPT for Dummies - Pam Baker Ccesa007.pdf
PDF
LDMMIA Reiki Yoga Finals Review Spring Summer
PDF
FORM 1 BIOLOGY MIND MAPS and their schemes
PPTX
TNA_Presentation-1-Final(SAVE)) (1).pptx
PDF
Vision Prelims GS PYQ Analysis 2011-2022 www.upscpdf.com.pdf
PDF
Environmental Education MCQ BD2EE - Share Source.pdf
PDF
Weekly quiz Compilation Jan -July 25.pdf
PPTX
Introduction to pro and eukaryotes and differences.pptx
PDF
Uderstanding digital marketing and marketing stratergie for engaging the digi...
PDF
My India Quiz Book_20210205121199924.pdf
PDF
Practical Manual AGRO-233 Principles and Practices of Natural Farming
PPTX
Share_Module_2_Power_conflict_and_negotiation.pptx
PPTX
20th Century Theater, Methods, History.pptx
احياء السادس العلمي - الفصل الثالث (التكاثر) منهج متميزين/كلية بغداد/موهوبين
1.3 FINAL REVISED K-10 PE and Health CG 2023 Grades 4-10 (1).pdf
B.Sc. DS Unit 2 Software Engineering.pptx
Soft-furnishing-By-Architect-A.F.M.Mohiuddin-Akhand.doc
advance database management system book.pdf
Virtual and Augmented Reality in Current Scenario
Cambridge-Practice-Tests-for-IELTS-12.docx
ChatGPT for Dummies - Pam Baker Ccesa007.pdf
LDMMIA Reiki Yoga Finals Review Spring Summer
FORM 1 BIOLOGY MIND MAPS and their schemes
TNA_Presentation-1-Final(SAVE)) (1).pptx
Vision Prelims GS PYQ Analysis 2011-2022 www.upscpdf.com.pdf
Environmental Education MCQ BD2EE - Share Source.pdf
Weekly quiz Compilation Jan -July 25.pdf
Introduction to pro and eukaryotes and differences.pptx
Uderstanding digital marketing and marketing stratergie for engaging the digi...
My India Quiz Book_20210205121199924.pdf
Practical Manual AGRO-233 Principles and Practices of Natural Farming
Share_Module_2_Power_conflict_and_negotiation.pptx
20th Century Theater, Methods, History.pptx

Streams or Loops? Java 8 Stream API by Niki Petkov - Proxiad Bulgaria

  • 1. Stream or Loop Java 8 Stream API Nikolay Petkov
  • 2. Java 8 Goodies ● Lambda expressions aka functional interfaces ● Method references ● Default methods
  • 6. Method Reference ● Static method ● Instance method of particular instance ● Instance method of particular type ● Constructor
  • 9. Default Method public interface Developer { void writeCode(); default void writeTest() { throw new ImPerfectException( “Why bother? My code is perfect”); } }
  • 10. External vs Internal Iteration for (Car car : cars) { if (“BMW”.equals(car.getBrand()) { car.setOwner(“Alex”) ; } } cars.forEach((car) -> { if (“BMW”.equals(car.getBrand()) { car.setOwner(“Alex”) ; } });
  • 11. Stream API Classes to support functional-style operations on streams of elements, such as map-reduce transformations on collections. int oldestBmwAge = cars.stream() .filter((car) -> “BMW”.equals(car.getBrand())) .mapToInt((car) -> car.getAge()) .max();
  • 12. Streams vs Collections ● Not a storage ● Functional by design ● Possibly unbounded ● Lazy by nature ● Use and discard
  • 13. Stream pipeline A stream pipeline consists of: ● a source (such as a Collection, an array, a generator function, or an I/O channel); ● followed by zero or more intermediate operations such as Stream.filter or Stream.map; ● and a terminal operation such as Stream.forEach or Stream.reduce. Execution begins when the terminal operation is invoked, and ends when the terminal operation completes
  • 14. Source ● Collection.stream() ● Arrays.stream(Object[]) ● Stream.of(Object[]) ● IntStream.range(int, int) ● BufferedReader.lines(); ● Files.list(Path dir) ● etc.
  • 15. Intermediate - filter Returns a stream consisting of the elements of this stream that match the given predicate. Stream<T> filter(Predicate<? super T> predicate) filter((String s) -> s.lenght() < 20) filter((String s) -> String::isEmpty))
  • 16. Intermediate - sorted Returns a stream consisting of the elements of this stream, sorted according to the provided Comparator. sorted(Comparator<? super T> comparator) sorted(comparing(Person::getLastName) sorted((s1, s2) -> s1.lenght() > s2.lenght())
  • 17. Intermediate - map Returns a stream consisting of the results of applying the given function to the elements of this stream. The map operation is kind of transformation of the stream elements to another elements. map(Function<T,R> mapper) mapToInt((String s) -> s.lenght()) map((User u) -> User::getRole())
  • 18. Terminal – max/min/sum/count Returns the maximum/minimum element of this stream according to the provided Comparator max(Comparator<? super T> comparator) max(comapring(Persone::getAge)) stream.mapToInt(Persone::getAge).sum()
  • 19. A lot more follows Questions?