Chapter 5 - Functional processing of collections (optional)) Flashcards

1
Q

describe
imperative programming

A

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;
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

features include:
1.Is a method of a collection
2.Will remove elements from the collection if the predicate returns true

A

give 2 features of the method
removeIf()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

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

A

give 3 examples of
reducing

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

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

explain in 3 steps how the stream method
map()
operates

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

syntax:

filter(lambda expression)

A

what is the syntax for the method
filter()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

a lambda contains one parameter and two statements

what is the syntax for writing this

A

the syntax is:

param1 -> {statement1; statement2;}

note: this rule applies the other way i.e 2param and 1 statement

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

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

A

give 3 reasons as to why
streams are usefull

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

the syntax is:

param1 -> {statement1; statement2;}

note: this rule applies the other way i.e 2param and 1 statement

A

a lambda contains one parameter and two statements

what is the syntax for writing this

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

a lambda contains one parameters and one statements

what is the syntax for writing this

A

the syntax is:

param1 -> statement 1

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

what is the syntax for the collection method
removeIf()
when used in a stream

A

syntax:

removeIf(predicateLambda)

example:

removeIf(element -> element.value > 10)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

the code is:

countries.forEach(country -> System.out.println(country));

A

write the code that uses the collections forEach() method and prints a collection of strings.

the collections name is countries

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

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

A

code:

animals.stream()
.filter(animal -> animal.getName().equals("elephant"))
.forEach(animal -> System.out.println(animal));
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

syntax:

limit(long maxSize)

parameters:
@param maxSize the number of elements the output stream should be limited to

A

what are the syntax and paramters of the stream method
limit()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

syntax:

Map(lambda expression)

A

what is the syntax for the stream method
map()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

how does the stream method
filter()
operate

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

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)

A

give 5 points concerning
lambdas

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

code:

animals.stream()
.filter(animal -> animal.getName().equals("elephant"))
.forEach(animal -> System.out.println(animal));
A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

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();

A

describe a
pipeline

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
19
Q

write the code that uses the collections forEach() method and prints a collection of strings.

the collections name is countries

A

the code is:

countries.forEach(country -> System.out.println(country));

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
20
Q

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

A

describe the method
map()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
21
Q

give 2 points describe
lambdas

A
  1. this is similar to a method except that it is more concise and has some pre defined rules
  2. these can be passed as arguments just as objects can be, they will then be stored and executed when required
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
22
Q

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

A

describe a
stream

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
23
Q

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

A

how does the stream method
filter()
operate

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
24
Q

syntax for this is:

(param1, param2) -> {statement1; statement2;}

A

a lambda contains two parameters and two statements

what is the syntax for writing this

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
25
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**
26
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)
27
describe how the collection method **removeIf()** works
this works by: If the predicate returns true then current element is removed from the colllection
28
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()**
29
A style of programming in which we specify what we want done rather than how it should be done. We are not concerned with how the result is achieved example: `.map(element -> element * 2)`
describe **declarative programming**
30
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()**
31
describe the method **limit()**
This is an intermediate stream method that will limit the number of elements in the stream to a given number.
32
describe a **predicate**
is a function that takes at least one argument and returns a boolean.
33
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
34
what is the syntax for the method **filter()**
syntax: `filter(lambda expression)`
35
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
36
syntax: `collection.stream()`
what is the syntax to **create a stream from a collection**
37
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
38
This is an intermediate stream method that will limit the number of elements in the stream to a given number.
describe the method **limit()**
39
the syntax is: param1 -> statement 1
a lambda contains one parameters and one statements what is the syntax for writing this
40
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.
41
what is the syntax for the stream method **count()**
syntax: `count()`
42
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()**
43
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
44
syntax: `removeIf(predicate)`
what is the syntax for the collection method **removeIf()**
45
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
46
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
47
this works by: If the predicate returns true then current element is removed from the colllection
describe how the collection method **removeIf()** works
48
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
49
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**
50
describe the method **count()**
This is a stream method that will return a count of the number of elements in a given stream
51
give 2 features of the stream method **count()**
features include: 1.Is a terminal operation 2.Takes no parameters
52
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
53
This is a method of a collection that can be used to remove elements that satisfy a given predicate
describe the method **removeIf()**
54
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()**
55
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**
56
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()**
57
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 temp = new ArrayList<>(); For(int element : collection){ Temp1 = Element * 2; Temp.add(temp1); } Return temp; ```
describe **imperative programming**
58
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()**
59
describe the method **removeIf()**
This is a method of a collection that can be used to remove elements that satisfy a given predicate
60
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()**
61
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
62
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)`
63
this is where we take a set of values and from them create a single value
describe the act of **reducing**
64
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
65
what is the syntax to **create a stream from a collection**
syntax: `collection.stream()`
66
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
67
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
68
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
69
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
70
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
71
what is the syntax for the collection method **removeIf()**
syntax: `removeIf(predicate)`
72
a lambda contains two parameters and two statements what is the syntax for writing this
syntax for this is: `(param1, param2) -> {statement1; statement2;}`
73
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**
74
syntax: `removeIf(predicateLambda)` example: `removeIf(element -> element.value > 10)`
what is the syntax for the collection method **removeIf()** when used in a stream
75
1. this is similar to a method except that it is more concise and has some pre defined rules 2. 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**
76
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
77
This is a stream method that will return a count of the number of elements in a given stream
describe the method **count()**
78
is a function that takes at least one argument and returns a boolean.
describe a **predicate**
79
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
80
syntax: `count()`
what is the syntax for the stream method **count()**
81
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
82
what is the syntax for the stream method **map()**
syntax: `Map(lambda expression)`
83
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**
84
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()**
85
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()**
86
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
87
features include: 1.Is a terminal operation 2.Takes no parameters
give 2 features of the stream method **count()**
88
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();`
89
describe the act of **reducing**
this is where we take a set of values and from them create a single value
90
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
91
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**
92
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**
93
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
94
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
95
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)
96
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
97
describe **declarative programming**
A style of programming in which we specify what we want done rather than how it should be done. We are not concerned with how the result is achieved example: `.map(element -> element * 2)`
98
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
99
[glossary quiz](https://learn2.open.ac.uk/mod/quiz/view.php?id=2014901)
ignore