Professional Documents
Culture Documents
Java 8 Feature Streams : Seminar
Java 8 Feature Streams : Seminar
Java 8 Feature Streams : Seminar
A
Seminar
submitted
in partial fulfillment
for the award of the Degree of
Bachelor of Technology
in Department of Computer Science & Engineering
I hereby declare that the work, which is being presented in the Seminar, entitled “Java 8 Feature
Streams” in partial fulfillment for the award of Degree of “Bachelor of Technology” in Computer
Science & Engineering and submitted to the Department of Computer Science & Engineering,
Jodhpur Institute of Engineering & Technology, Rajasthan Technical University is a record of my own
WORK carried under the Guidance of Ms. Vinita Mathur, Associate Prof., Department of Computer
Science & Engineering.
Payal Tikyani
Computer Science
University Roll No: 15EJICS080
Jodhpur Institute of Engineering & Technology
Counter Signed by
II
ACKNOWLEDGEMENT
It gives me immense pleasure and satisfaction in presenting this seminar report on the research topic
named “Java 8 Feature Streams”. I would like to take this opportunity to express my Sincere gratitude
to several people, without whose help and encouragement, it would had been impossible for me to
carry out the desired work.
I would like to express my sincere thanks to Ms. Vinita Mathur, seminar guide, for her invaluable
guidance throughout the research work. I am indebted for her guidance, encouragement, understanding
and insightful support in the documentation process and overall guidance.
Last but not the least I would like to thank my friends for their support and continuous encouragement
throughout the work.
Payal Tikyani
15EJICS080
Computer Science & Engineering
III
ABSTRACT
The Java Stream API provides a functional approach to processing collections of objects. The Java
Stream API was added in Java 8 along with several other functional programming features.
This chapter discusses the ways for creating streams in Java 8 with different use of them.
IV
TABLE OF CONTENTS
Acknowledgment _________________________________________________________iii
Abstract_ __________________________________________________________________iv
Table of Contents____________________________________________________________v
List of Tables_______________________________________________________________vi
List of Figures______________________________________________________________vii
1. Introduction ________________________________________________________________1
2. Literature survey____________________________________________________________2
References_____________________________________________________________________11
V
INTRODUCTION
Java 8 introduced Java Streams which allows us to express data processing queries in a short functional and
elegant way.
Simply put, streams are wrappers around a data source, allowing us to operate with that data source and making
bulk processing convenient and fast.
A stream does not store data and, in that sense, is not a data structure. It also never modifies the underlying data
source.
A short definition is “a sequence of elements from a source that supports aggregate operations.” Let‟s break it
down:
Sequence of elements: A stream provides an interface to a sequenced set of values of a specific element
type. However, streams don‟t actually store elements; they are computed on demand.
Source: Streams consume from a data-providing source such as collections, arrays, or I/O resources.
Aggregate operations: Streams support SQL-like operations and common operations from functional
programing languages, such as filter, map, reduce, find, match, sorted, and so on.
Furthermore, stream operations have two fundamental characteristics that make them very different from
collection operations:
Pipelining: Many stream operations return a stream themselves. This allows operations to be chained to
form a larger pipeline. This enables certain optimizations, such as laziness and short-circuiting.
Internal iteration: In contrast to collections, which are iterated explicitly (external iteration), stream
operations do the iteration behind the scenes for you.
1
LITERATURE SURVEY
Java 8 Lambdas in Action is probably the best book to learn Java 8 features. It's one of the clearly-
written guides to Java 8 lambdas and functional programming in Java. It begins with a practical
introduction to the structure and benefits of lambda expressions in real-world Java code.
The book then introduces the Stream API and shows how it can make the collections-related code
radically easier to understand and maintain.
This article shows that a stream supports many operations such as filter, map, reduce, and iterate that
can be combined to write concise and expressive data processing queries. This new way of writing
code is very different from how we would process collections before Java SE 8.
2
CHAPTERS DESCRIBING THE WORK
Collection interface has been extended with stream() and parallelStream() default methods to get the Stream
for sequential and parallel execution.
Simply put, streams are wrappers around a data source, allowing us to operate with that data source and
making bulk processing convenient and fast.
A stream does not store data and, in that sense, is not a data structure. It also never modifies the underlying
data source.
A Java Stream is a component that is capable of internal iteration of its elements, meaning it can iterate its
elements itself. In contrast, when we are using the Java Collections iteration features (e.g a Java Iterator or
the Java for-each loop used with a Java Iterable) we have to implement the iteration of the elements by
ourself.
A short definition is “a sequence of elements from a source that supports aggregate operations.” Let‟s break it
down:
Furthermore, stream operations have two fundamental characteristics that make them very different from
collection operations:
Pipelining: Many stream operations return a stream themselves. This allows operations to be chained
to form a larger pipeline. This enables certain optimizations, such as laziness and short-circuiting.
Internal iteration: In contrast to collections, which are iterated explicitly (external iteration), stream
operations do the iteration behind the scenes for you.
3
Stream Processing:-
We can attach listeners to a Stream. These listeners are called when the Stream iterates the elements internally. The
listeners are called once for each element in the stream. That way each listener gets to process each element in the
stream. This is referred to as stream processing.
The listeners of a stream form a chain. The first listener in the chain can process the element in the stream, and
then return a new element for the next listener in the chain to process. A listener can either return the same element
or a new, depending on what the purpose of that listener (processor) is.
Classification of streams:-
Input and output streams:-
Based on the direction of data flow, streams can be categorized as input streams and output streams.
Based on the type of data we can classify streams as byte streams and text streams (aka character streams).
Byte streams:-
o All byte stream classes are descended from InputStream and OutputStream.
o Class names of byte streams end with the word stream as in FileInputStream and FileOutputStream.
o All character stream classes are descended from Reader and Writer.
o Class names of text streams end with the word „Reader‟ or „Writer‟ as in FileReader and FileWriter.
o Java stores character values using Unicode conventions. Text streams automatically convert the
Unicode internal format to and from the local character set.
Wrapper streams:-
One stream may wrap around another stream to add some functionality to the inner stream or to make the
inner stream do some low level tasks.
Filter streams wrap around other streams and transform the data providing additional functionality.
4
Subclasses of FilterInputStream are BufferedInputStream, CheckedInputStream, CipherInputStream,
DataInputStream, DeflaterInputStream, DigestInputStream, InflaterInputStream, LineNumberInputStream,
ProgressMonitorInputStream and PushbackInputStream.
Markable streams:-
Markable streams provide the capability to mark a position in the stream and then later reset the stream so that it
can be reread from the marked position.
The mark() method marks a position in the stream and it takes the buffer capacity as an integer parameter.
o The buffer memory is used to keep track of the data between the mark and the current position and
when this buffer memory is exceeded, the mark becomes invalid.
The reset() method simply repositions the stream to its last marked position.
5
Chapter 2 : Creation of Streams
To get started with Streams, we need a method to create them. Here are five ways to create a Stream in Java 8.
Iterate
Iterate accepts an initial element and a function which is applied to the previous element to produce the
next element.
Generate
Generate accepts a Supplier to generate an infinite Stream of unsorted elements. This method is good for
generating a Stream of random elements.
6
Chapter 3: Operations on Streams
The Stream interface in java.util .stream.Stream defines many operations, which can be grouped in two categories.
filter, sorted, and map, which can be connected together to form a pipeline.
collect, which closed the pipeline and returned a result.
Stream operations that can be connected are called intermediate operations. They can be connected together
because their return type is a Stream. Operations that close a stream pipeline are called terminal operations. They
produce a result from a pipeline such as a List, an Integer, or even void (any non-Stream type).
This distinction of operations is important. Well, intermediate operations do not perform any processing until a
terminal operation is invoked on the stream pipeline; they are “lazy.” This is because intermediate operations can
usually be “merged” and processed into a single pass by the terminal operation.
Java 8 Stream intermediate operations return another Stream which allows you to call multiple operations in a form
of a query. Stream intermediate operations do not get executed until a terminal operation is invoked. All
Intermediate operations are lazy, so they‟re not executed until a result of a processing is actually needed. Traversal
of the Stream does not begin until the terminal operation of the pipeline is executed.
Here is the list of all Stream intermediate operations:
filter()
map()
flatMap()
distinct()
sorted()
peek()
limit()
skip()
Filtering:-
There are several operations that can be used to filter elements from a stream:-
filter(Predicate): Takes a predicate (java.util.function.Predicate) as an argument and returns a stream
including all elements that match the given predicate
distinct: Returns a stream with unique elements (according to the implementation of equals for a stream
element)
limit(n): Returns a stream that is no longer than the given size n
skip(n): Returns a stream with the first n number of elements discarded
Mapping:-
Streams support the method map, which takes a function (java.util.function.Function) as an argument to project the
elements of a stream into another form. The function is applied to each element, “mapping” it into a new element.
7
Java-8 Stream terminal operations produces a non-stream, result such as primitive value, a collection or no value at
all. Terminal operations are typically preceded by intermediate operations which return another Stream which
allows operations to be connected in a form of a query.
Here is the list of all Stream terminal operations:-
toArray()
collect()
count()
reduce()
forEach()
forEachOrdered()
min()
max()
anyMatch()
allMatch()
noneMatch()
findAny()
findFirst()
Reducing:-
The Java Stream reduce() method is a terminal operation that can reduce all elements in the stream to a single
element.
8
Chapter 4: Parallel Streams
If the source of stream is something different than a Collection or an array, the parallel() method
should be used:
Under the hood, Stream API automatically uses the ForkJoin framework to execute operations in
parallel. By default, the common thread pool will be used and there is no way to assign some custom
thread pool to it. This can be overcome by using a custom set of parallel collectors.
When using streams in parallel mode, avoid blocking operations and use parallel mode when tasks
need the similar amount of time to execute (if one task lasts much longer than the other, it can slow
down the complete app‟s workflow).
The stream in parallel mode can be converted back to the sequential mode by using
the sequential() method:
9
CONCLUSION
Java SE 8 introduces the Streams API, which lets us express sophisticated data processing queries. We‟ve seen
that a stream supports many operations such as filter, map, reduce, and iterate that can be combined to write
concise and expressive data processing queries. This new way of writing code is very different from how we
would process collections before Java SE 8. However, it has many benefits. First, the Streams API makes use of
several techniques such as laziness and short-circuiting to optimize our data processing queries. Second, streams
can be parallelized automatically to leverage multicore architectures.
We saw various operations supported and how lambdas and pipelines can be used to write concise code. We
also saw some characteristics of streams like lazy evaluation, parallel and infinite streams.
The Stream API is a powerful but simple to understand set of tools for processing sequence of elements. It
allows us to reduce a huge amount of boilerplate code, create more readable programs and improve app‟s
productivity when used properly.
10
REFERENCES
1. https://www.oracle.com/technetwork/articles/java/ma14-java-se-8-streams-2177646.html
2. https://dzone.com/articles/a-guide-to-streams-in-java-8-in-depth-tutorial-wit
3. https://www.journaldev.com/2389/java-8-features-with-examples
4. https://www.youtube.com/watch?v=b0On2gN020k&list=PLsyeobzWxl7otduRddQWYTQezVul0xI
X6&index=14
11