Day19 StreamApi
Day19 StreamApi
Q1. Which one of these is the correct Java Lambda Expression Syntax ?
C () -> {body}
D (argument-list) . {body}
Q1. Which one of these is the correct Java Lambda Expression Syntax ?
C () -> {body}
D (argument-list) . {body}
A Argument List
B Arrow Token
C Body
D All
A Argument List
B Arrow Token
C Body
D All
● Enhances the usability of Java Collection types, making it easy to iterate and perform tasks against
each element in the collection
● Supports sequential and parallel aggregate operations
● Stream API allows developers process data in a declarative way
● Not a data structure:Streams have no storage. They carry values from a source
through a pipeline of operations.
● Designed for lambdas :All Stream operations take lambdas as arguments
● Do not support indexed access :
● Can easily be output as arrays or Lists
● Lazy :Many Stream operations are postponed until it is known how much data is
eventually needed
● Parallelizable :If you designate a Stream as parallel, then operations on it will
automatically
● Can be unbounded:Unlike with collections, you can designate a generator
function, and clients can consume entries as long as they want, with values being
generated on the fly
● The operations which return non-stream values such as primitive or object are called
terminal operations.
● Furthermore, unlike intermediate operations, we can’t chain them together.
● They produce the end result.
● Once a terminal operation completes, the Stream is no longer valid.
● Hence, we can’t use that stream again.
● For example : forEach(), toArray(), reduce(), collect(), min(), max(), count(),
anyMatch(), allMatch(), noneMatch(), findFirst(), findAny()
● Stream processing consists of a series of zero or more intermediate operations
followed by a terminal operation.
● Each intermediate operation returns a new stream. The terminal operation returns
something other than a stream.
● When our code doesn’t work as expected, either we add a println() statement or go with
setting a breakpoint to see the values of an object.
● Since intermediate operations don’t return something until needed, printing is not a
straightforward process with the streams.
● Therefore, we need to apply some particular approaches to print the values.
s.forEach(System.out::println);
System.out.println(s.collect(Collectors.toList()));
s.limit(4).forEach(System.out::println);
s.peek(System.out::println).count();
TNS India Foundation | ‹#›
Partners in Economic Transformation
Mapping
● The Stream interface's map method maps each element of stream with the result of
passing the element to a function.
● Map() 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.
Syntax:
● The map method returns a new Stream of elements whose type may be different from
the type of the elements of the current stream.
iimport java.util.Arrays;
import java.util.List;
Q2. Which method is used to transform each element of a Stream using a provided
function?
A filter
B map
C reduce
D filterMap
Q2. Which method is used to transform each element of a Stream using a provided
function?
A filter
B map
C reduce
D filterMap
Problem statement:
You are working on a social media analytics platform where you need to analyze user posts.
Each post has a content string and a timestamp. Your task is to implement a Java program
that performs the following tasks:
1. Create a list of Post objects, where each Post has a content string and a timestamp.
2. Filter out any posts that were posted before a specific date and time.
3. Map each remaining post to its content.
4. Split the content of each post into individual words.
5. Count the frequency of each word across all the posts.
6. Collect the word frequencies into a map, where the word is the key and the frequency is
the value.