Chapter 5 - Functional processing of collections (optional)) Flashcards
describe
imperative programming
this programming style is where we write a series of statements/commands that each change the change the state of an object.
We are concerned and know how each step is taken to achieve the result
example:
ArrayList<int> temp = new ArrayList<>(); For(int element : collection){ Temp1 = Element * 2; Temp.add(temp1); } Return temp;
features include:
1.Is a method of a collection
2.Will remove elements from the collection if the predicate returns true
give 2 features of the method
removeIf()
examples include:
1.adding up all values from the stream
2.Concatenating characters from a stream to create a string
3.Picking the smallest or largest value from a stream
give 3 examples of
reducing
this operates by:
1.Takes a stream as input
2.Creates a new element from the original element (can be a change in type or content)
3.Creates a new stream as its output
explain in 3 steps how the stream method
map()
operates
syntax:
filter(lambda expression)
what is the syntax for the method
filter()
a lambda contains one parameter and two statements
what is the syntax for writing this
the syntax is:
param1 -> {statement1; statement2;}
note: this rule applies the other way i.e 2param and 1 statement
reasons include:
1.They provide methods that we might commonly carry out on collections
2.these are safe when applying parallel processing
3.Using these means that we do not need to explicitly create a loop
give 3 reasons as to why
streams are usefull
the syntax is:
param1 -> {statement1; statement2;}
note: this rule applies the other way i.e 2param and 1 statement
a lambda contains one parameter and two statements
what is the syntax for writing this
a lambda contains one parameters and one statements
what is the syntax for writing this
the syntax is:
param1 -> statement 1
what is the syntax for the collection method
removeIf()
when used in a stream
syntax:
removeIf(predicateLambda)
example:
removeIf(element -> element.value > 10)
the code is:
countries.forEach(country -> System.out.println(country));
write the code that uses the collections forEach() method and prints a collection of strings.
the collections name is countries
write the following code:
1. create a stream from the collection animals
2. filter out the animals with name elephant
3. print the name of the filtered animals
animals has a getter method named getName() that returns a string
code:
animals.stream() .filter(animal -> animal.getName().equals("elephant")) .forEach(animal -> System.out.println(animal));
syntax:
limit(long maxSize)
parameters:
@param maxSize the number of elements the output stream should be limited to
what are the syntax and paramters of the stream method
limit()
syntax:
Map(lambda expression)
what is the syntax for the stream method
map()
how does the stream method
filter()
operate
This operates by:
1.Taking a stream as input
2.Evaluating each element of the stream (only an evaluation to true will get past the filter)
3.Creating a new stream as output that is composed of those that survived step 2
these include:
1.Lamdas are code but are not executed immediately
2.Lamdas do not belong to a class like methods do
3.There is no access modifier a lambda is assumed public
4.Lamdas do not declare a return type. Instead it is the job of the compiler to work this out
5.Lambdas do not have names (anonymous function)
give 5 points concerning
lambdas
code:
animals.stream() .filter(animal -> animal.getName().equals("elephant")) .forEach(animal -> System.out.println(animal));
write the following code:
1. create a stream from the collection animals
2. filter out the animals with name elephant
3. print the name of the filtered animals
animals has a getter method named getName() that returns a string
this is composed of two or more stream operations which are chained together.
1.a source of the stream
2.at least one intermediate stream operation
3.a terminal stream operation
syntax:
Source().inter1().inter2().inter3().terminal();
describe a
pipeline
write the code that uses the collections forEach() method and prints a collection of strings.
the collections name is countries
the code is:
countries.forEach(country -> System.out.println(country));
this is a stream operation that will take an element and using that element transform it into a specific piece of data we are interested in
describe the method
map()
give 2 points describe
lambdas
- this is similar to a method except that it is more concise and has some pre defined rules
- these can be passed as arguments just as objects can be, they will then be stored and executed when required
these are an alternative way of processing collections. they provide their own methods that allow us to manipulate data in a functional style of programming
describe a
stream
This operates by:
1.Taking a stream as input
2.Evaluating each element of the stream (only an evaluation to true will get past the filter)
3.Creating a new stream as output that is composed of those that survived step 2
how does the stream method
filter()
operate
syntax for this is:
(param1, param2) -> {statement1; statement2;}
a lambda contains two parameters and two statements
what is the syntax for writing this
these include:
1. usefull for simple, one off tasks that do not require the complexity of a full class
2. can be helpfull when creating declarative/functional style programs
give 2
use cases for lambdas
how do we
return data from a stream
When we wish to return data from a stream we must put return at the start of the stream
Example:
Return collection.stream()
.filter(lambda)
.map(lambda)
.reduce(identity, lambda)
describe how the collection method
removeIf()
works
this works by:
If the predicate returns true then current element is removed from the colllection
this is a method that is built into java collections. it is used so that we can iterate the collection and perform some operation on each element
describe the method
forEach()
A style of programming in which we specifywhatwe want done rather thanhowit should be done.
We are not concerned with how the result is achieved
example:
.map(element -> element * 2)
describe
declarative programming
this is a stream method that will take all data coming from a stream and callapse it into a single value
describe the method
reduce()
describe the method
limit()
This is an intermediate stream method that will limit the number of elements in the stream to a given number.
describe a
predicate
is afunctionthat takes at least oneargumentand returns a boolean.
describe the
difference between a method and a function
the difference between these is that a method will belong to an object where as a function does not
what is the syntax for the method
filter()
syntax:
filter(lambda expression)
give 3 reasons as to why
streams are usefull
reasons include:
1.They provide methods that we might commonly carry out on collections
2.these are safe when applying parallel processing
3.Using these means that we do not need to explicitly create a loop
syntax:
collection.stream()
what is the syntax to
create a stream from a collection
describe the method
map()
this is a stream operation that will take an element and using that element transform it into a specific piece of data we are interested in
This is an intermediate stream method that will limit the number of elements in the stream to a given number.
describe the method
limit()
the syntax is:
param1 -> statement 1
a lambda contains one parameters and one statements
what is the syntax for writing this
describe
functional programming
this style of programming is a form of declarative programming and uses a series of functions that will each change/transform data and then return it, allowing allow the next function to work on the transformed data.
what is the syntax for the stream method
count()
syntax:
count()
this includes:
1.The for each methods accepts the lambda
2.It will then pass each element of the collection to the lambdas parameter
3.The lambda accepts the element given in the parameter and executes its code in the body
in 3 steps describe the execution process of the collection method
forEach()
describe the method
reduce()
this is a stream method that will take all data coming from a stream and callapse it into a single value
syntax:
removeIf(predicate)
what is the syntax for the collection method
removeIf()
give 4 points regarding the method
filter()
points include:
1.Is an intermediate operation
2.The lambda expression that is given as an argument is known as a predicate
3.The output stream may be less than or equal to the input stream (depending on whether anything was filtered)
4.Does not change the collection the stream is coming from
this operates by:
1.takes a starting value that it passes to the lambda along with the first piece of data in the stream
2.The lambda then performs an addition operation for example on both and keeps its result in the parameter
3.The same process happens again but instead the value generated from the last lambda operation is used as the start value
in 3 steps describe how the stream method
reduce()
operates
this works by:
If the predicate returns true then current element is removed from the colllection
describe how the collection method
removeIf()
works
explain in 3 steps how the stream method
map()
operates
this operates by:
1.Takes a stream as input
2.Creates a new element from the original element (can be a change in type or content)
3.Creates a new stream as its output
a method that is part of a pipeline and will receive a stream as input but will have a non stream output
describe a
Terminal operation
describe the method
count()
This is a stream method that will return a count of the number of elements in a given stream
give 2 features of the stream method
count()
features include:
1.Is a terminal operation
2.Takes no parameters
describe a
Terminal operation
a method that is part of a pipeline and will receive a stream as input but will have a non stream output
This is a method of a collection that can be used to remove elements that satisfy a given predicate
describe the method
removeIf()
this is a stream operation that can filter out unwanted elements and only let the elements that we are interested in continue on in the stream
describe the method
filter()
this style of programming is a form of declarative programming and uses a series of functions that will each change/transform data and then return it, allowing allow the next function to work on the transformed data.
describe
functional programming
features include:
1.Is an intermediate operation
2.The size of the input stream will be identical to the output stream
3.can be used to extract or convert data from the element in the stream
give 3 features of the stream method
map()
this programming style is where we write a series of statements/commands that each change the change the state of an object.
We are concerned and know how each step is taken to achieve the result
example:
ArrayList<int> temp = new ArrayList<>(); For(int element : collection){ Temp1 = Element * 2; Temp.add(temp1); } Return temp;
describe
imperative programming
features include:
1.This is a terminal operation
2.The starting value of the reduce method is known as the identity
3.is used so that we can obtain a single value from many data values
give 3 features of the stream method
reduce()
describe the method
removeIf()
This is a method of a collection that can be used to remove elements that satisfy a given predicate
syntax:
reduce(identity, lambda)
parameters:
identity - the value we will start with for the reducing such as (0, 0.0, “”)
lambda - a lambda expression that has two parameters being:
1. a parameter that acts as a counter for the running total (is initialised by the value of identity)
2. a parameter that will take each element from the stream
explain the syntax and parameters for the stream method
reduce()
describe the method
filter()
this is a stream operation that can filter out unwanted elements and only let the elements that we are interested in continue on in the stream
what are the 2 ways in which we can apply the collection method
forEach()
this can be applied:
1. directly on a collection - collection.forEach(lambdaExpression)
2. as the terminal operator on a stream - collection.stream().forEach(lambdaExpression)
this is where we take a set of values and from them create a single value
describe the act of
reducing
what are the syntax and paramters of the stream method
limit()
syntax:
limit(long maxSize)
parameters:
@param maxSize the number of elements the output stream should be limited to
what is the syntax to
create a stream from a collection
syntax:
collection.stream()
in 3 steps describe the execution process of the collection method
forEach()
this includes:
1.The for each methods accepts the lambda
2.It will then pass each element of the collection to the lambdas parameter
3.The lambda accepts the element given in the parameter and executes its code in the body
explain the syntax and parameters for the stream method
reduce()
syntax:
reduce(identity, lambda)
parameters:
identity - the value we will start with for the reducing such as (0, 0.0, “”)
lambda - a lambda expression that has two parameters being:
1. a parameter that acts as a counter for the running total (is initialised by the value of identity)
2. a parameter that will take each element from the stream
in 3 steps describe how the stream method
reduce()
operates
this operates by:
1.takes a starting value that it passes to the lambda along with the first piece of data in the stream
2.The lambda then performs an addition operation for example on both and keeps its result in the parameter
3.The same process happens again but instead the value generated from the last lambda operation is used as the start value
give 3 features of the stream method
reduce()
features include:
1.This is a terminal operation
2.The starting value of the reduce method is known as the identity
3.is used so that we can obtain a single value from many data values
give 3 points concerning
streams
these include:
1.The elements cannot be accessed via an index but are accessed sequentially
2.The content and ordering cannot be changed. If the content is to be changed then a new stream must be created from the stream
3.A stream is potentially infinite
what is the syntax for the collection method
removeIf()
syntax:
removeIf(predicate)
a lambda contains two parameters and two statements
what is the syntax for writing this
syntax for this is:
(param1, param2) -> {statement1; statement2;}
When we wish to return data from a stream we must put return at the start of the stream
Example:
Return collection.stream()
.filter(lambda)
.map(lambda)
.reduce(identity, lambda)
how do we
return data from a stream
syntax:
removeIf(predicateLambda)
example:
removeIf(element -> element.value > 10)
what is the syntax for the collection method
removeIf()
when used in a stream
- this is similar to a method except that it is more concise and has some pre defined rules
- these can be passed as arguments just as objects can be, they will then be stored and executed when required
give 2 points describe
lambdas
give 3 features of the stream method
map()
features include:
1.Is an intermediate operation
2.The size of the input stream will be identical to the output stream
3.can be used to extract or convert data from the element in the stream
This is a stream method that will return a count of the number of elements in a given stream
describe the method
count()
is afunctionthat takes at least oneargumentand returns a boolean.
describe a
predicate
describe a
stream
these are an alternative way of processing collections. they provide their own methods that allow us to manipulate data in a functional style of programming
syntax:
count()
what is the syntax for the stream method
count()
describe the method
forEach()
this is a method that is built into java collections. it is used so that we can iterate the collection and perform some operation on each element
what is the syntax for the stream method
map()
syntax:
Map(lambda expression)
these include:
1.The elements cannot be accessed via an index but are accessed sequentially
2.The content and ordering cannot be changed. If the content is to be changed then a new stream must be created from the stream
3.A stream is potentially infinite
give 3 points concerning
streams
this can be applied:
1. directly on a collection - collection.forEach(lambdaExpression)
2. as the terminal operator on a stream - collection.stream().forEach(lambdaExpression)
what are the 2 ways in which we can apply the collection method
forEach()
points include:
1.Is an intermediate operation
2.The lambda expression that is given as an argument is known as a predicate
3.The output stream may be less than or equal to the input stream (depending on whether anything was filtered)
4.Does not change the collection the stream is coming from
give 4 points regarding the method
filter()
how does a stream operate
this operates by extracting each element in turn from a collection and passing them on to another function for processing
features include:
1.Is a terminal operation
2.Takes no parameters
give 2 features of the stream method
count()
describe a
pipeline
this is composed of two or more stream operations which are chained together.
1.a source of the stream
2.at least one intermediate stream operation
3.a terminal stream operation
syntax:
Source().inter1().inter2().inter3().terminal();
describe the act of
reducing
this is where we take a set of values and from them create a single value
this operates by extracting each element in turn from a collection and passing them on to another function for processing
how does a stream operate
the difference between these is that a method will belong to an object where as a function does not
describe the
difference between a method and a function
a method that is part of a pipeline and will have a stream as input and a new stream as output
describe an
Intermediate operation
give 3 examples of
reducing
examples include:
1.adding up all values from the stream
2.Concatenating characters from a stream to create a string
3.Picking the smallest or largest value from a stream
give 2 features of the method
removeIf()
features include:
1.Is a method of a collection
2.Will remove elements from the collection if the predicate returns true
give 5 points concerning
lambdas
these include:
1.Lamdas are code but are not executed immediately
2.Lamdas do not belong to a class like methods do
3.There is no access modifier a lambda is assumed public
4.Lamdas do not declare a return type. Instead it is the job of the compiler to work this out
5.Lambdas do not have names (anonymous function)
describe an
Intermediate operation
a method that is part of a pipeline and will have a stream as input and a new stream as output
describe
declarative programming
A style of programming in which we specifywhatwe want done rather thanhowit should be done.
We are not concerned with how the result is achieved
example:
.map(element -> element * 2)
give 2
use cases for lambdas
these include:
1. usefull for simple, one off tasks that do not require the complexity of a full class
2. can be helpfull when creating declarative/functional style programs
ignore