Java 8
Streams and Collection Framework
By Aamna Zahid
Agenda
Streams
Code related to streams
Collection Framework
Summarize
Collection Framework
The Collection framework
represents a unified
architecture for storing It provides readymade
A collection represents architecture. It
and manipulating a group
a group of objects, represents a set of
of objects. It has:
known as its elements. 1.Interfaces and its classes and interfaces.
implementations, i.e.,
classes
2.Algorithm
List Interface
List interface is the child interface of Collection interface. It inhibits a list type
data structure in which we can store the ordered collection of objects. It can
have duplicate values.
List interface is implemented by the classes ArrayList, LinkedList, Vector, and
Stack.
Queue Interface
Queue interface maintains the first-in-first-out order. It can be defined as an
ordered list that is used to hold the elements which are about to be processed.
There are various classes like PriorityQueue, Deque, and ArrayDeque which
implements the Queue interface.
Set Interface
Set Interface in Java is present in java.util package. It extends the Collection
interface. It represents the unordered set of elements that doesn't allow us to
store the duplicate items.
We can store at most one null value in Set. Set is implemented by HashSet,
LinkedHashSet, and TreeSet.
Streams
In Java, streams are the sequence of data that are read from the source and
written to the destination.
Streams are designed to be functional, meaning that they allow you to perform
operations on data in a way that is declarative and concise, without having to
write loops or iterate over the data manually. This makes it easier to write code
that is more readable, maintainable, and less error-prone.
Cont..
Using stream, you can process data in a declarative way similar to SQL statements.
For example, consider the following SQL statement.
SELECT max(salary), employee_id, employee_name FROM Employee
The above SQL expression automatically returns the maximum salaried employee's
details, without doing any computation on the developer's end.
Using collections framework in Java, a developer has to use loops and make repeated
checks.
Java 8 introduced the concept of the stream that lets the developer to process data
declaratively.
Cont..
• 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.
Cont..
Cont..
• List<Employee> employees =
List<Employee> employees = ... // Your list of Employee objects
Employee maxSalaryEmployee = employees.stream()
.max(Comparator.comparing(Employee::getSalary))
.orElse(null);
Lamda expression..
(a,b) -> a+b
(int a , int b) ->a+b
Summarize
Java Streams and the Collection Framework empower developers to manipulate and
process data with concise, expressive, and functional programming paradigms.
Java Streams and the Collection Framework provide powerful tools for efficient data
manipulation and processing, enabling developers to unleash the full potential of their
applications with elegance and simplicity.