fizbuzz.java
· 947 B · Java
Bruto
import java.util.*;
import java.util.function.Predicate;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
// wowey, wow, wow.
public class Main {
public static void main(String[] args) {
int[] arr = IntStream.rangeClosed(1, 100).toArray();
Map<Predicate<Integer>, String> preds = new LinkedHashMap<>();
preds.put(x -> x % 3 == 0, "Fizz");
preds.put(x -> x % 5 == 0, "Buzz");
System.out.println(fizzBuzz(arr, preds));
}
private static String fizzBuzz(int[] arr, Map<Predicate<Integer>, String> preds) {
return Arrays.stream(arr)
.mapToObj(x -> preds.entrySet().stream()
.filter(entry -> entry.getKey().test(x))
.map(Map.Entry::getValue)
.reduce(String::concat)
.orElse(String.valueOf(x)))
.collect(Collectors.joining(" "));
}
}
1 | import java.util.*; |
2 | import java.util.function.Predicate; |
3 | import java.util.stream.Collectors; |
4 | import java.util.stream.IntStream; |
5 | |
6 | // wowey, wow, wow. |
7 | |
8 | public class Main { |
9 | public static void main(String[] args) { |
10 | int[] arr = IntStream.rangeClosed(1, 100).toArray(); |
11 | |
12 | Map<Predicate<Integer>, String> preds = new LinkedHashMap<>(); |
13 | preds.put(x -> x % 3 == 0, "Fizz"); |
14 | preds.put(x -> x % 5 == 0, "Buzz"); |
15 | |
16 | System.out.println(fizzBuzz(arr, preds)); |
17 | } |
18 | |
19 | private static String fizzBuzz(int[] arr, Map<Predicate<Integer>, String> preds) { |
20 | return Arrays.stream(arr) |
21 | .mapToObj(x -> preds.entrySet().stream() |
22 | .filter(entry -> entry.getKey().test(x)) |
23 | .map(Map.Entry::getValue) |
24 | .reduce(String::concat) |
25 | .orElse(String.valueOf(x))) |
26 | .collect(Collectors.joining(" ")); |
27 | } |
28 | } |
29 | |
30 |