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

## StreamReduce
### Instructions
Create a file `StreamReduce.java`.
CON-1881-Review-java-piscine-subjects-Fix-grammar-and-semantic-issues (#2102) * docs(java piscine): Review and update AdventureAbstract exercise * docs(java piscine): Review AdventureCharacter subject * docs(java piscine): Review AdventureInterface subject * docs(java piscine): Review AdventureMonster subject * docs(java piscine): review AdventureSorcerer subject * docs(java piscine): Review AdventureTemplar subject * docs(java piscine): Review AdventureUtils subject * docs(java piscine): Review AdventureWeapon subject * docs(java piscine): Review ComputeArray subject * docs(java piscine): Review FileSearch subject * docs(java piscine): Review FormatDate subject * docs(java piscine): Review KeepTheChange subject * docs(java piscine): Review ListEquals subject * docs(java piscine): Review MapInventory subject * docs(java piscine): Review Observer subject * docs(java piscine): Review Palindrome subject * docs(java piscine): Review RegexReplace subject * docs(java piscine): Review Singleton subject * docs(java piscine): Review StarConstructors subject * docs(java piscine): Review StarGalaxy subject * docs(java piscine): Review StarMass subject * docs(java piscine): Review StarOverride subject * docs(java piscine): Review StarPlanet subject * docs(java piscine): Review StarProperties subject * docs(java piscine): Review StarStatic subject * docs(java piscine): Review StarUtils subject * docs(java piscine): Review StreamCollect subject * docs(java piscine): Review StreamMap subject * docs(java piscine): Review StreamReduce subject * docs(java piscine): Review StringConcat subject * docs(java piscine): Review StringContains subject * docs(java piscine): Review Wedding subject * docs(java piscine): Review WeddingComplex subject * docs(java piscine): Change a part of RegexReplace subject * docs(java piscine): Update AdventureAbstract subject * docs(java piscine): Fix wrong naming parameter -> attribute * docs(java piscine): Remove empty lines * docs(java piscine): Remove ## from line
1 year ago
Create a function `sumAll` which returns the sum of integers in the stream.
Create a function `divideAndAddElements` which sums the result of the division, of all the integers in the stream, by 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)