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
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