Suche senden
Hochladen
JDK8 Streams
•
Als PPTX, PDF herunterladen
•
0 gefällt mir
•
261 views
Bansilal Haudakari
Folgen
JDK8 Streams Overview
Weniger lesen
Mehr lesen
Software
Melden
Teilen
Melden
Teilen
1 von 16
Jetzt herunterladen
Empfohlen
Apouc 2014-java-8-create-the-future
Apouc 2014-java-8-create-the-future
OUGTH Oracle User Group in Thailand
Lambdas and-streams-s ritter-v3
Lambdas and-streams-s ritter-v3
Simon Ritter
Lambdas Hands On Lab
Lambdas Hands On Lab
Simon Ritter
JDK8 Lambdas and Streams: Changing The Way You Think When Developing Java
JDK8 Lambdas and Streams: Changing The Way You Think When Developing Java
Simon Ritter
Lambdas : Beyond The Basics
Lambdas : Beyond The Basics
Simon Ritter
Java SE 8
Java SE 8
Simon Ritter
Lessons Learnt With Lambdas and Streams in JDK 8
Lessons Learnt With Lambdas and Streams in JDK 8
Simon Ritter
Improved Developer Productivity In JDK8
Improved Developer Productivity In JDK8
Simon Ritter
Empfohlen
Apouc 2014-java-8-create-the-future
Apouc 2014-java-8-create-the-future
OUGTH Oracle User Group in Thailand
Lambdas and-streams-s ritter-v3
Lambdas and-streams-s ritter-v3
Simon Ritter
Lambdas Hands On Lab
Lambdas Hands On Lab
Simon Ritter
JDK8 Lambdas and Streams: Changing The Way You Think When Developing Java
JDK8 Lambdas and Streams: Changing The Way You Think When Developing Java
Simon Ritter
Lambdas : Beyond The Basics
Lambdas : Beyond The Basics
Simon Ritter
Java SE 8
Java SE 8
Simon Ritter
Lessons Learnt With Lambdas and Streams in JDK 8
Lessons Learnt With Lambdas and Streams in JDK 8
Simon Ritter
Improved Developer Productivity In JDK8
Improved Developer Productivity In JDK8
Simon Ritter
Java 8 Features
Java 8 Features
Leninkumar Koppoju
Java 8 features
Java 8 features
Oleg Tsal-Tsalko
Functional programming with_jdk8-s_ritter
Functional programming with_jdk8-s_ritter
Simon Ritter
Project Jigsaw in JDK9
Project Jigsaw in JDK9
Simon Ritter
Java- Updates in java8-Mazenet solution
Java- Updates in java8-Mazenet solution
Mazenetsolution
Java 8
Java 8
Sudipta K Paik
Java SE 8 - New Features
Java SE 8 - New Features
Naveen Hegde
Introduction to new features in java 8
Introduction to new features in java 8
New York City College of Technology Computer Systems Technology Colloquium
Java 8 - Features Overview
Java 8 - Features Overview
Sergii Stets
The Road to Lambda - Mike Duigou
The Road to Lambda - Mike Duigou
jaxconf
Major Java 8 features
Major Java 8 features
Sanjoy Kumar Roy
Collections
Collections
Marwa Dosoky
The Dark Side Of Lambda Expressions in Java 8
The Dark Side Of Lambda Expressions in Java 8
Takipi
Java 8 - Project Lambda
Java 8 - Project Lambda
Rahman USTA
Lambdas in Java 8
Lambdas in Java 8
Tobias Coetzee
BigDataSpain 2016: Stream Processing Applications with Apache Apex
BigDataSpain 2016: Stream Processing Applications with Apache Apex
Thomas Weise
Language Integrated Query - LINQ
Language Integrated Query - LINQ
Doncho Minkov
Refactoring to Scala DSLs and LiftOff 2009 Recap
Refactoring to Scala DSLs and LiftOff 2009 Recap
Dave Orme
Quick introduction to scala
Quick introduction to scala
Mohammad Hossein Rimaz
A Brief, but Dense, Intro to Scala
A Brief, but Dense, Intro to Scala
Derek Chen-Becker
JDK 8
JDK 8
Maciej Dragan
new features in jdk8
new features in jdk8
岩 夏
Weitere ähnliche Inhalte
Was ist angesagt?
Java 8 Features
Java 8 Features
Leninkumar Koppoju
Java 8 features
Java 8 features
Oleg Tsal-Tsalko
Functional programming with_jdk8-s_ritter
Functional programming with_jdk8-s_ritter
Simon Ritter
Project Jigsaw in JDK9
Project Jigsaw in JDK9
Simon Ritter
Java- Updates in java8-Mazenet solution
Java- Updates in java8-Mazenet solution
Mazenetsolution
Java 8
Java 8
Sudipta K Paik
Java SE 8 - New Features
Java SE 8 - New Features
Naveen Hegde
Introduction to new features in java 8
Introduction to new features in java 8
New York City College of Technology Computer Systems Technology Colloquium
Java 8 - Features Overview
Java 8 - Features Overview
Sergii Stets
The Road to Lambda - Mike Duigou
The Road to Lambda - Mike Duigou
jaxconf
Major Java 8 features
Major Java 8 features
Sanjoy Kumar Roy
Collections
Collections
Marwa Dosoky
The Dark Side Of Lambda Expressions in Java 8
The Dark Side Of Lambda Expressions in Java 8
Takipi
Java 8 - Project Lambda
Java 8 - Project Lambda
Rahman USTA
Lambdas in Java 8
Lambdas in Java 8
Tobias Coetzee
BigDataSpain 2016: Stream Processing Applications with Apache Apex
BigDataSpain 2016: Stream Processing Applications with Apache Apex
Thomas Weise
Language Integrated Query - LINQ
Language Integrated Query - LINQ
Doncho Minkov
Refactoring to Scala DSLs and LiftOff 2009 Recap
Refactoring to Scala DSLs and LiftOff 2009 Recap
Dave Orme
Quick introduction to scala
Quick introduction to scala
Mohammad Hossein Rimaz
A Brief, but Dense, Intro to Scala
A Brief, but Dense, Intro to Scala
Derek Chen-Becker
Was ist angesagt?
(20)
Java 8 Features
Java 8 Features
Java 8 features
Java 8 features
Functional programming with_jdk8-s_ritter
Functional programming with_jdk8-s_ritter
Project Jigsaw in JDK9
Project Jigsaw in JDK9
Java- Updates in java8-Mazenet solution
Java- Updates in java8-Mazenet solution
Java 8
Java 8
Java SE 8 - New Features
Java SE 8 - New Features
Introduction to new features in java 8
Introduction to new features in java 8
Java 8 - Features Overview
Java 8 - Features Overview
The Road to Lambda - Mike Duigou
The Road to Lambda - Mike Duigou
Major Java 8 features
Major Java 8 features
Collections
Collections
The Dark Side Of Lambda Expressions in Java 8
The Dark Side Of Lambda Expressions in Java 8
Java 8 - Project Lambda
Java 8 - Project Lambda
Lambdas in Java 8
Lambdas in Java 8
BigDataSpain 2016: Stream Processing Applications with Apache Apex
BigDataSpain 2016: Stream Processing Applications with Apache Apex
Language Integrated Query - LINQ
Language Integrated Query - LINQ
Refactoring to Scala DSLs and LiftOff 2009 Recap
Refactoring to Scala DSLs and LiftOff 2009 Recap
Quick introduction to scala
Quick introduction to scala
A Brief, but Dense, Intro to Scala
A Brief, but Dense, Intro to Scala
Andere mochten auch
JDK 8
JDK 8
Maciej Dragan
new features in jdk8
new features in jdk8
岩 夏
Java 7 new features
Java 7 new features
Shivam Goel
New features in Java 7
New features in Java 7
Girish Manwani
New Features Of JDK 7
New Features Of JDK 7
Deniz Oguz
Java 5 and 6 New Features
Java 5 and 6 New Features
Jussi Pohjolainen
New Features in JDK 8
New Features in JDK 8
Martin Toshev
Andere mochten auch
(7)
JDK 8
JDK 8
new features in jdk8
new features in jdk8
Java 7 new features
Java 7 new features
New features in Java 7
New features in Java 7
New Features Of JDK 7
New Features Of JDK 7
Java 5 and 6 New Features
Java 5 and 6 New Features
New Features in JDK 8
New Features in JDK 8
Ähnlich wie JDK8 Streams
Functional Programming With Lambdas and Streams in JDK8
Functional Programming With Lambdas and Streams in JDK8
IndicThreads
Lambdas And Streams Hands On Lab, JavaOne 2014
Lambdas And Streams Hands On Lab, JavaOne 2014
Simon Ritter
Lambdas and Streams in Java SE 8: Making Bulk Operations simple - Simon Ritter
Lambdas and Streams in Java SE 8: Making Bulk Operations simple - Simon Ritter
JAXLondon2014
Lambdas And Streams in JDK8
Lambdas And Streams in JDK8
Simon Ritter
What's New in Java 8
What's New in Java 8
javafxpert
Lambda.pdf
Lambda.pdf
ManishWalia18
A Brief Conceptual Introduction to Functional Java 8 and its API
A Brief Conceptual Introduction to Functional Java 8 and its API
Jörn Guy Süß JGS
Servlet 4.0 Adopt-a-JSR 10 Minute Infodeck
Servlet 4.0 Adopt-a-JSR 10 Minute Infodeck
Edward Burns
Java Library for High Speed Streaming Data
Java Library for High Speed Streaming Data
Oracle Developers
Functional reactive programming
Functional reactive programming
Araf Karsh Hamid
Belfast JUG 23-10-2013
Belfast JUG 23-10-2013
eamonnlong
FOSDEM19 MySQL Component Infrastructure
FOSDEM19 MySQL Component Infrastructure
Georgi Kodinov
Sumo Logic QuickStart Webinar - Jan 2016
Sumo Logic QuickStart Webinar - Jan 2016
Sumo Logic
Java8
Java8
Felipe Mamud
Java 8 streams
Java 8 streams
Srinivasan Raghvan
Super simple introduction to REST-APIs (2nd version)
Super simple introduction to REST-APIs (2nd version)
Patrick Savalle
Charles Sharp: Java 8 Streams
Charles Sharp: Java 8 Streams
jessitron
Lambdas & Streams
Lambdas & Streams
C4Media
Project Lambda: Functional Programming Constructs in Java - Simon Ritter (Ora...
Project Lambda: Functional Programming Constructs in Java - Simon Ritter (Ora...
jaxLondonConference
Microservices Part 4: Functional Reactive Programming
Microservices Part 4: Functional Reactive Programming
Araf Karsh Hamid
Ähnlich wie JDK8 Streams
(20)
Functional Programming With Lambdas and Streams in JDK8
Functional Programming With Lambdas and Streams in JDK8
Lambdas And Streams Hands On Lab, JavaOne 2014
Lambdas And Streams Hands On Lab, JavaOne 2014
Lambdas and Streams in Java SE 8: Making Bulk Operations simple - Simon Ritter
Lambdas and Streams in Java SE 8: Making Bulk Operations simple - Simon Ritter
Lambdas And Streams in JDK8
Lambdas And Streams in JDK8
What's New in Java 8
What's New in Java 8
Lambda.pdf
Lambda.pdf
A Brief Conceptual Introduction to Functional Java 8 and its API
A Brief Conceptual Introduction to Functional Java 8 and its API
Servlet 4.0 Adopt-a-JSR 10 Minute Infodeck
Servlet 4.0 Adopt-a-JSR 10 Minute Infodeck
Java Library for High Speed Streaming Data
Java Library for High Speed Streaming Data
Functional reactive programming
Functional reactive programming
Belfast JUG 23-10-2013
Belfast JUG 23-10-2013
FOSDEM19 MySQL Component Infrastructure
FOSDEM19 MySQL Component Infrastructure
Sumo Logic QuickStart Webinar - Jan 2016
Sumo Logic QuickStart Webinar - Jan 2016
Java8
Java8
Java 8 streams
Java 8 streams
Super simple introduction to REST-APIs (2nd version)
Super simple introduction to REST-APIs (2nd version)
Charles Sharp: Java 8 Streams
Charles Sharp: Java 8 Streams
Lambdas & Streams
Lambdas & Streams
Project Lambda: Functional Programming Constructs in Java - Simon Ritter (Ora...
Project Lambda: Functional Programming Constructs in Java - Simon Ritter (Ora...
Microservices Part 4: Functional Reactive Programming
Microservices Part 4: Functional Reactive Programming
Kürzlich hochgeladen
Vip Call Girls Noida ➡️ Delhi ➡️ 9999965857 No Advance 24HRS Live
Vip Call Girls Noida ➡️ Delhi ➡️ 9999965857 No Advance 24HRS Live
Call Girls In Delhi Whatsup 9873940964 Enjoy Unlimited Pleasure
Call Girls In Mukherjee Nagar 📱 9999965857 🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SE...
Call Girls In Mukherjee Nagar 📱 9999965857 🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SE...
Call Girls In Delhi Whatsup 9873940964 Enjoy Unlimited Pleasure
Clustering techniques data mining book ....
Clustering techniques data mining book ....
ShaimaaMohamedGalal
Unlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language Models
aagamshah0812
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
panagenda
A Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docx
ComplianceQuest1
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
kellynguyen01
Software Quality Assurance Interview Questions
Software Quality Assurance Interview Questions
Arshad QA
Microsoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdf
Willy Marroquin (WillyDevNET)
Diamond Application Development Crafting Solutions with Precision
Diamond Application Development Crafting Solutions with Precision
SolGuruz
Optimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTV
shikhaohhpro
Right Money Management App For Your Financial Goals
Right Money Management App For Your Financial Goals
Jhone kinadey
Active Directory Penetration Testing, cionsystems.com.pdf
Active Directory Penetration Testing, cionsystems.com.pdf
Cionsystems
Test Automation Strategy for Frontend and Backend
Test Automation Strategy for Frontend and Backend
Arshad QA
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
kalichargn70th171
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
Delhi Call girls
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Alberto González Trastoy
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽❤️🧑🏻 89...
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽❤️🧑🏻 89...
gurkirankumar98700
Salesforce Certified Field Service Consultant
Salesforce Certified Field Service Consultant
AxelRicardoTrocheRiq
Professional Resume Template for Software Developers
Professional Resume Template for Software Developers
Vinodh Ram
Kürzlich hochgeladen
(20)
Vip Call Girls Noida ➡️ Delhi ➡️ 9999965857 No Advance 24HRS Live
Vip Call Girls Noida ➡️ Delhi ➡️ 9999965857 No Advance 24HRS Live
Call Girls In Mukherjee Nagar 📱 9999965857 🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SE...
Call Girls In Mukherjee Nagar 📱 9999965857 🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SE...
Clustering techniques data mining book ....
Clustering techniques data mining book ....
Unlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language Models
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
A Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docx
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Software Quality Assurance Interview Questions
Software Quality Assurance Interview Questions
Microsoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdf
Diamond Application Development Crafting Solutions with Precision
Diamond Application Development Crafting Solutions with Precision
Optimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTV
Right Money Management App For Your Financial Goals
Right Money Management App For Your Financial Goals
Active Directory Penetration Testing, cionsystems.com.pdf
Active Directory Penetration Testing, cionsystems.com.pdf
Test Automation Strategy for Frontend and Backend
Test Automation Strategy for Frontend and Backend
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽❤️🧑🏻 89...
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽❤️🧑🏻 89...
Salesforce Certified Field Service Consultant
Salesforce Certified Field Service Consultant
Professional Resume Template for Software Developers
Professional Resume Template for Software Developers
JDK8 Streams
1.
Copyright © 2015,
Oracle and/or its affiliates. All rights reserved. | JDK8 : Streams Bansi Haudakari Oracle Confidential – Internal/Restricted/Highly Restricted 1
2.
Copyright © 2015,
Oracle and/or its affiliates. All rights reserved. | Agenda 8 Features of JDK 8 Why Streams How Streams Work Streams Internals Lets have some Fun With Hands-On Session 1 2 3 4 5 Oracle Confidential – Internal/Restricted/Highly Restricted 2
3.
Copyright © 2015,
Oracle and/or its affiliates. All rights reserved. | JDK8 Features Oracle Confidential – Internal/Restricted/Highly Restricted 3 Feature Lambda Expressions Generic Type Changes & Improvements Stream API New Date & Time APIs
4.
Copyright © 2015,
Oracle and/or its affiliates. All rights reserved. | 8 Features of JDK8 Oracle Confidential – Internal/Restricted/Highly Restricted 4 Feature Type Annotations Optional References StampedLocks
5.
Copyright © 2015,
Oracle and/or its affiliates. All rights reserved. | Other Features Oracle Confidential – Internal/Restricted/Highly Restricted 5 Feature Secure Random Generator Controlling OS Processes Concurrency Updates
6.
Copyright © 2015,
Oracle and/or its affiliates. All rights reserved. | Why Stream API • Find sum of all integers greater than 10 – private static int sumIterator(List<Integer> list) { Iterator<Integer> it = list.iterator(); int sum = 0; while (it.hasNext()) { int num = it.next(); if (num > 10) { sum += num; } } return sum; } Problems: • Client program has to Iterate over the List : External Iteration • Sequential In Nature. • Lots of code for simple task Oracle Confidential – Internal/Restricted/Highly Restricted 6
7.
Copyright © 2015,
Oracle and/or its affiliates. All rights reserved. | Why Stream API • Stream API implements Internal Iteration because its better for framework code/library to take care of Iteration • Internal Iteration provides several features such as sequential and parallel execution, filtering based on the given criteria, mapping etc. • Most of the Stream API method arguments are functional interfaces, so lambda expressions work very well with them. • Parallelism: – Fork/Join framework introduced in Java SE 7, has efficient machinery to implement parallel operations in your applications. – But implementation is a complex task & error-prone; and if not done right; might lead to multi-threading bugs having potential to crash the application – With Internal Iteration, we got the possibility of operations to be done in parallel. • Anytime you want particular job to run in multiple threads in parallel cores, all you have to do is call parallelStream() method instead of stream() method. Oracle Confidential – Internal/Restricted/Highly Restricted 7
8.
Copyright © 2015,
Oracle and/or its affiliates. All rights reserved. | Design Rationale Behind Streams API • Why are Stream operations not defined directly on Collection? • Mixing two different kinds of methods on the same abstraction. For example, strings.removeAll(s -> s.length() == 0); // remove all empty String objects from the collection strings.filter(s -> s.length() == 0); // return a stream containing all the non-empty Strings would have surprisingly different results; • If lazy methods added to Collection, results in confusion about whether the collection was in “lazy mode” or “eager mode” • The more methods added to Collection, the greater the chance of name collisions with existing third- party implementations. • Collection would have many new methods to produce sequential streams/parallel streams. • user experience with this design led to a more formal separation of the “stream” methods into their own abstraction Rather than burdening Collection with new and different functionality, it is cleaner to provide a Stream view with the new functionality; Oracle Confidential – Internal/Restricted/Highly Restricted 8
9.
Copyright © 2015,
Oracle and/or its affiliates. All rights reserved. | Stream API Overview • Stream API interfaces and classes are in java.util.stream package • Stream<T> : represents a Stream of object References •Streams of primitives : IntStream, LongStream & DoubleStream • The Stream core methods have been divided into 2 parts: • Intermediate operations: ofilter(Predicate), map(Function), reduce(BinaryOperator), sorted() • Terminal operations: oforEach(), collect(), match(),reduce(), osum(), min(), max(), count() • Short-circuit operations: anyMatch(), allMatch(), findFirst() Oracle Confidential – Internal/Restricted/Highly Restricted 9
10.
Copyright © 2015,
Oracle and/or its affiliates. All rights reserved. | Functional Interfaces In Stream • Function and BiFunction: takes one type of argument T and returns another type of argument R. Stream methods where Function is used are: –<R> Stream<R> map(Function<? super T, ? extends R> mapper) –<U> U reduce(U identity, BiFunction<U, ? super T, U> accumulator, BinaryOperator<U> combiner) •Predicate and BiPredicate : represents a predicate against which elements of the stream are tested. Stream methods where Predicate is used are: –Stream<T> filter(Predicate<? super T> predicate) •Consumer and BiConsumer: represents an operation that accepts a single input argument and returns no result. Stream methods where Consumer is used are: –void forEach(Consumer<? super T> action) •Supplier: represent an operation through which we can generate new values in the stream –<R> R collect(Supplier<R> supplier,BiConsumer<R, ? super T> accumulator,BiConsumer<R, R> combiner) Oracle Confidential – Internal/Restricted/Highly Restricted 10
11.
Copyright © 2015,
Oracle and/or its affiliates. All rights reserved. | What Are Streams? • Streams are Monads, playing a big part in bringing functional programming to Java • A Monad is a structure that represents “Computations defined as sequences of steps”. • A type with a Monad structure defines what it means to “chain operations, or nest functions” of that type together. • What is Stream API? • Databases and other programming languages allow us to specify “Aggregate operations” explicitly e.g. SUM, AVG, COUNT. The streams API provides this mechanism in the Java platform • The stream API makes use of lambdas and extension methods • Streams can be applied on collections, arrays, IO streams and generator functions • Streams can be finite or infinite • Streams can apply intermediate functions on the data that produce another stream (e.g. map, reduce) • Stream operations are chained together into pipelines • Streams are lazy: computation is performed when the terminal operation is invoked Oracle Confidential – Internal/Restricted/Highly Restricted 11
12.
Copyright © 2015,
Oracle and/or its affiliates. All rights reserved. | How Streams Work? • Collection is an in-memory data structure to hold data • Stream is neither a data structure nor a data-store. • Stream performs computations on-demand. • Stream operates on the source data structure (collection and array) and produce pipelined data that can be used to perform specific operations. Example: create a stream from the list and filter it based on a condition. • Stream operations use functional interfaces, lambda expressions. • Stream internal iteration principle helps in lazy-seeking some of the stream operations, example filtering, mapping, or duplicate removal can be implemented lazily, allowing higher performance • Streams are consumable, so there is no way to create a reference to stream for future usage. Since the data is on-demand, it’s not possible to reuse the same stream multiple times. Oracle Confidential – Internal/Restricted/Highly Restricted 12
13.
Copyright © 2015,
Oracle and/or its affiliates. All rights reserved. | Stream In Action Creating Streams: • Use Stream.of() to create a stream from similar type of data. For example – Stream<Integer> stream = Stream.of(1,2,3,4); • use Stream.of() with an array of Objects to return the stream – Stream<Integer> stream = Stream.of(new Integer[]{1,2,3,4}); • use Collection stream() to create sequential stream and parallelStream() to create parallel stream. – Stream<Integer> sequentialStream = myList.stream(); – Stream<Integer> parallelStream = myList.parallelStream(); • use Stream.generate() and Stream.iterate() methods to create Stream – Stream<String> stream1 = Stream.generate(() -> {return "abc";}); – Stream<String> stream2 = Stream.iterate("abc", (i) -> i); Oracle Confidential – Internal/Restricted/Highly Restricted 13
14.
Copyright © 2015,
Oracle and/or its affiliates. All rights reserved. | Stream In Action How to Get A Collection or Array From Stream: • Use Stream collect() method to get List, Map or Set from stream 1) Stream<Integer> intStream = Stream.of(1,2,3,4); List<Integer> intList = intStream.collect(Collectors.toList()); 2)Map<Integer,Integer> intMap = intStream.collect(Collectors.toMap(i -> i, i -> i+10)); System.out.println(intMap); //prints {1=11, 2=12, 3=13, 4=14} • Use stream toArray() method to create an array from the stream. – Integer[] intArray = intStream.toArray(Integer[]::new); Oracle Confidential – Internal/Restricted/Highly Restricted 14
15.
Copyright © 2015,
Oracle and/or its affiliates. All rights reserved. | Stream In Action : Intermediate Operations • Use filter() method to test stream elements for a condition and generate filtered list. • Use map() to apply functions to an stream. Example apply upper case function to a list of Strings. System.out.println(names.map(s -> { return s.toUpperCase(); }).collect(Collectors.toList())); • Use sorted() to sort the stream elements by passing Comparator argument. List<String> naturalSorted = names3.sorted().collect(Collectors.toList()); • Use flatMap() to create a stream from the stream of list. Stream<List<String>> namesOriginalList = Stream.of(Arrays.asList("David", "Lisa")); Stream<String> flatStream = namesOriginalList .flatMap(strList -> strList.stream()); flatStream.forEach(System.out::println); Oracle Confidential – Internal/Restricted/Highly Restricted 15
16.
Copyright © 2015,
Oracle and/or its affiliates. All rights reserved. | Stream In Action : Terminal Operations • Use reduce() to perform a reduction on the elements of the stream, using an associative accumulation function, and return an Optional. E.g. multiply the integers in a stream. Stream<Integer> numbers = Stream.of(1,2,3,4,5); Optional<Integer> intOptional = numbers.reduce((i,j) -> {return i*j;}); if(intOptional.isPresent()) System.out.println("Multiplication = "+intOptional.get()); //120 • Use forEach() for iterating over the stream • Use match() for matching elements in the Stream • Use findFirst() for finding the first string from a stream Oracle Confidential – Internal/Restricted/Highly Restricted 16
Jetzt herunterladen