-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAppliedArithmeticsAnotherVersion.java
More file actions
38 lines (35 loc) · 1.57 KB
/
AppliedArithmeticsAnotherVersion.java
File metadata and controls
38 lines (35 loc) · 1.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
package Exercises;
import java.util.Arrays;
import java.util.List;
import java.util.Scanner;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.stream.Collectors;
public class AppliedArithmeticsAnotherVersion {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
List<Integer> list = Arrays.stream(scan.nextLine().split("\\s+")).map(Integer::parseInt).collect(Collectors.toList());
Function<List<Integer>, List<Integer>> add = listOfInt -> listOfInt.stream().map(e -> e += 1).collect(Collectors.toList());
Function<List<Integer>, List<Integer>> multiply = listOfInt -> listOfInt.stream().map(e -> e *= 2).collect(Collectors.toList());
Function<List<Integer>, List<Integer>> subtract = listOfInt -> listOfInt.stream().map(e -> e -= 1).collect(Collectors.toList());
Consumer<Integer> print = e -> System.out.print(e + " ");
String command = scan.nextLine();
while (!command.equals("end")) {
switch (command) {
case "add":
list = add.apply(list);
break;
case "multiply":
list = multiply.apply(list);
break;
case "subtract":
list = subtract.apply(list);
break;
case "print":
list.stream().forEach(print);
System.out.println();
}
command = scan.nextLine();
}
}
}