mirror of https://github.com/01-edu/public.git
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
56 lines
1.4 KiB
56 lines
1.4 KiB
1 year ago
|
## StreamReduce
|
||
|
|
||
|
### Instructions
|
||
|
|
||
|
Create a file `StreamReduce.java`.
|
||
|
|
||
|
Create a function `sumAll` which returns sum of integers in the stream.
|
||
|
Create e function `divideAndAddElements` which sum the result of the division between all the integers in the stream and the divider.
|
||
|
|
||
|
|
||
|
### Expected Functions
|
||
|
```java
|
||
|
public class StreamReduce {
|
||
|
public static Integer sumAll(Stream<Integer> s) {
|
||
|
// your code here
|
||
|
}
|
||
|
|
||
|
public static Integer divideAndAddElements(Stream<Integer> s, int divider) {
|
||
|
// your code here
|
||
|
}
|
||
|
}
|
||
|
```
|
||
|
|
||
|
### Usage
|
||
|
|
||
|
Here is a possible ExerciseRunner.java to test your function :
|
||
|
|
||
|
```java
|
||
|
import java.io.IOException;
|
||
|
import java.util.stream.Stream;
|
||
|
|
||
|
public class ExerciseRunner {
|
||
|
public static void main(String[] args) throws IOException {
|
||
|
System.out.println(StreamReduce.sumAll(Stream.of(3, 5, 7, 10)));
|
||
|
System.out.println(StreamReduce.sumAll(Stream.of()));
|
||
|
System.out.println(StreamReduce.divideAndAddElements(Stream.of(3, 5, 7, 10), 2));
|
||
|
System.out.println(StreamReduce.divideAndAddElements(Stream.of(), 2));
|
||
|
}
|
||
|
}
|
||
|
```
|
||
|
|
||
|
and its output :
|
||
|
```shell
|
||
|
$ javac *.java -d build
|
||
|
$ java -cp build ExerciseRunner
|
||
|
25
|
||
|
0
|
||
|
11
|
||
|
0
|
||
|
$
|
||
|
```
|
||
|
|
||
|
### Notions
|
||
|
[Stream](https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/util/stream/Stream.html)
|
||
|
[Reduce](https://www.baeldung.com/java-stream-reduce)
|