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
Q

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

A

give 2
use cases for lambdas

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

how do we
return data from a stream

A

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

describe how the collection method
removeIf()
works

A

this works by:

If the predicate returns true then current element is removed from the colllection

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

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

A

describe the method
forEach()

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

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)

A

describe
declarative programming

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

this is a stream method that will take all data coming from a stream and callapse it into a single value

A

describe the method
reduce()

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

describe the method
limit()

A

This is an intermediate stream method that will limit the number of elements in the stream to a given number.

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

describe a
predicate

A

is afunctionthat takes at least oneargumentand returns a boolean.

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

describe the
difference between a method and a function

A

the difference between these is that a method will belong to an object where as a function does not

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

what is the syntax for the method
filter()

A

syntax:

filter(lambda expression)

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

give 3 reasons as to why
streams are usefull

A

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

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

syntax:

collection.stream()

A

what is the syntax to
create a stream from a collection

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

describe the method
map()

A

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

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

This is an intermediate stream method that will limit the number of elements in the stream to a given number.

A

describe the method
limit()

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

the syntax is:

param1 -> statement 1

A

a lambda contains one parameters and one statements

what is the syntax for writing this

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

describe
functional programming

A

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
Q

what is the syntax for the stream method
count()

A

syntax:

count()

42
Q

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

A

in 3 steps describe the execution process of the collection method
forEach()

43
Q

describe the method
reduce()

A

this is a stream method that will take all data coming from a stream and callapse it into a single value

44
Q

syntax:

removeIf(predicate)

A

what is the syntax for the collection method
removeIf()

45
Q

give 4 points regarding the method
filter()

A

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
Q

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

A

in 3 steps describe how the stream method
reduce()
operates

47
Q

this works by:

If the predicate returns true then current element is removed from the colllection

A

describe how the collection method
removeIf()
works

48
Q

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

A

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
Q

a method that is part of a pipeline and will receive a stream as input but will have a non stream output

A

describe a
Terminal operation

50
Q

describe the method
count()

A

This is a stream method that will return a count of the number of elements in a given stream

51
Q

give 2 features of the stream method
count()

A

features include:
1.Is a terminal operation
2.Takes no parameters

52
Q

describe a
Terminal operation

A

a method that is part of a pipeline and will receive a stream as input but will have a non stream output

53
Q

This is a method of a collection that can be used to remove elements that satisfy a given predicate

A

describe the method
removeIf()

54
Q

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

A

describe the method
filter()

55
Q

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.

A

describe
functional programming

56
Q

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

A

give 3 features of the stream method
map()

57
Q

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

describe
imperative programming

58
Q

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

A

give 3 features of the stream method
reduce()

59
Q

describe the method
removeIf()

A

This is a method of a collection that can be used to remove elements that satisfy a given predicate

60
Q

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

A

explain the syntax and parameters for the stream method
reduce()

61
Q

describe the method
filter()

A

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
Q

what are the 2 ways in which we can apply the collection method
forEach()

A

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
Q

this is where we take a set of values and from them create a single value

A

describe the act of
reducing

64
Q

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

A

syntax:

limit(long maxSize)

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

65
Q

what is the syntax to
create a stream from a collection

A

syntax:

collection.stream()

66
Q

in 3 steps describe the execution process of the collection method
forEach()

A

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
Q

explain the syntax and parameters for the stream method
reduce()

A

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
Q

in 3 steps describe how the stream method
reduce()
operates

A

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
Q

give 3 features of the stream method
reduce()

A

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
Q

give 3 points concerning
streams

A

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
Q

what is the syntax for the collection method
removeIf()

A

syntax:

removeIf(predicate)

72
Q

a lambda contains two parameters and two statements

what is the syntax for writing this

A

syntax for this is:

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

73
Q

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)

A

how do we
return data from a stream

74
Q

syntax:

removeIf(predicateLambda)

example:

removeIf(element -> element.value > 10)

A

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

75
Q
  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
A

give 2 points describe
lambdas

76
Q

give 3 features of the stream method
map()

A

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
Q

This is a stream method that will return a count of the number of elements in a given stream

A

describe the method
count()

78
Q

is afunctionthat takes at least oneargumentand returns a boolean.

A

describe a
predicate

79
Q

describe a
stream

A

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
Q

syntax:

count()

A

what is the syntax for the stream method
count()

81
Q

describe the method
forEach()

A

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
Q

what is the syntax for the stream method
map()

A

syntax:

Map(lambda expression)

83
Q

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

A

give 3 points concerning
streams

84
Q

this can be applied:
1. directly on a collection - collection.forEach(lambdaExpression)
2. as the terminal operator on a stream - collection.stream().forEach(lambdaExpression)

A

what are the 2 ways in which we can apply the collection method
forEach()

85
Q

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

A

give 4 points regarding the method
filter()

86
Q

how does a stream operate

A

this operates by extracting each element in turn from a collection and passing them on to another function for processing

87
Q

features include:
1.Is a terminal operation
2.Takes no parameters

A

give 2 features of the stream method
count()

88
Q

describe a
pipeline

A

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
Q

describe the act of
reducing

A

this is where we take a set of values and from them create a single value

90
Q

this operates by extracting each element in turn from a collection and passing them on to another function for processing

A

how does a stream operate

91
Q

the difference between these is that a method will belong to an object where as a function does not

A

describe the
difference between a method and a function

92
Q

a method that is part of a pipeline and will have a stream as input and a new stream as output

A

describe an
Intermediate operation

93
Q

give 3 examples of
reducing

A

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
Q

give 2 features of the method
removeIf()

A

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

95
Q

give 5 points concerning
lambdas

A

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
Q

describe an
Intermediate operation

A

a method that is part of a pipeline and will have a stream as input and a new stream as output

97
Q

describe
declarative programming

A

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)

98
Q

give 2
use cases for lambdas

A

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
A

ignore