:: Enseignements :: ESIPE :: IMAC2 :: IMAC2 2020-2021 :: Object oriented programming in Java ::
![[LOGO]](http://igm.univ-mlv.fr/ens/resources/mlv.png) |
#10.en Lambda, method reference and stream
|
Exercice 1 - 'The math adds up'
The purpose of this exercise is to find out how to use
lambdas and streams.
-
We are trying to count the number of occurrences of a word in a list.
var list = List.of("hello", "world", "hello", "lambda");
System.out.println(count(list, "hello")); // 2
Write the code of the count method knowing that the counter
is a long integer.
-
We are looking to write a semantically equivalent count2 method (which does the same thing) to count but using the API of
Stream.
How do I get a Stream from an object of type List ?
The idea here is to filter the stream to keep only equal words
to the word passed in parameter then to count these.
What are the methods for filtering a stream and counting the number of elements, respectively?
The method used to filter takes an object of type Predicate<T>
as a parameter. In our case, what is the type corresponding to T ?
Indicate the code allowing to create a lambda filtering on the word passed in parameter which can be declared as Predicate
Write the code for the count2 method.
Exercice 2 - UpperCase
The purpose of this exercise is to find out how to use,
in addition to lambdas and streams, "method references".
-
We are trying to write a method taking as a parameter a list of character strings and returning a new list containing the character strings in uppercase.
var list = List.of("hello", "world", "hello", "lambda");
System.out.println(upperCase(list)); // [HELLO, WORLD, HELLO, LAMBDA]
Write the upperCase method (first) without using the Stream API.
-
We are now looking to write a method upperCase2 doing the same thing but with a Stream .
How can we use the Stream.map method here?
To store the result in a new list, the idea is to create
list then add each word to the list.
public static List<String> upperCase2(List<String> words) {
var uppercases = new ArrayList<String>();
...
to request the addition, we will use the forEach method on the stream.
Write the code for the upperCase 2 method using lambdas.
-
In fact, instead of using lambdas, it is possible in this example to use the syntax of method references with the operator ::
(coloncolon).
Write a method upperCase3 which uses the syntax of
method reference.
-
In fact, instead of using forEach , it is better (no side effect) to use the collect method with the Collector
returned by the method Collectors.toList().
Write an upperCase4 method using the collector
Collectors.toList().
Exercice 3 - Counting on a discount
The purpose of this exercise is to find out how
perform a reduction on a stream.
During the first exercise, we used the count method which returns a long integer. We now want to write a new count3 method that returns an integer on 32 bits.
For this, once the words have been filtered, we will transform
(with map ) each word in 1 (the number) then we will,
with the reduce method, do the aggregation of values.
-
Explain why we are not going to use the map method but the mapToInt method?
-
Write the code for the method method count3 .
Exercice 4 - Speed evaluation
We are trying to find out which is faster among the 3 ways to write
count .
For this we will use a list defined by the following code
var list2 =
new Random(0)
.ints(1_000_000, 0, 100)
.mapToObj(Integer::toString)
.collect(Collectors.toList());
-
Explain what the local variable list2 contains.
-
We are looking to write a printAndTime method allowing
to calculate the execution time of the call to the count method on the list2 .
To calculate the execution time, we ask the time before then the time after and if we subtract the two times, we find the execution time.
var start = System.nanoTime();
... // faire le calcul
var end = System.nanoTime();
System.out.println("result " + result);
System.out.println(" elapsed time " + (end - start));
Write the code of printAndTime.
-
We also want to calculate the execution time with
other methods, like count2 for example.
How not to duplicate the code for calculating the execution time?
Which functional interface should we use knowing that we are going to call
printAndTime as follows?
printAndTime(() -> count(list2, "33"));
printAndTime(() -> count2(list2, "33"));
printAndTime(() -> count3(list2, "33"));
Write the new code of printAndTime.
-
Explain the results you get.
Exercice 5 - Games Of Streams
File
StreamsTest.java declares some methods
with a comment that describes what they are intended to compute and return as result.
Use the API
java.util.stream.Stream to provide an implementation for each of them.
© Université de Marne-la-Vallée