fizbuzz.java
· 947 B · Java
Исходник
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 |
leonfizzbuzz.java
· 1.3 KiB · Java
Исходник
package com.github.curriculeon;
import java.util.*;
import java.util.function.Predicate;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
// And https://github.com/Git-Leon personal memory optimizations of above.
// https://github.com/Git-Leon/FizzBuzz/blob/master/src/main/java/com/github/curriculeon/Main.java
public class Main {
public static void main(String[] args) {
System.out.println(
fizzBuzz(
IntStream.rangeClosed(1, 100).toArray(),
new LinkedHashMap<Predicate<Integer>, String>() {{
put(x1 -> x1 % 3 == 0, "Fizz");
put(x -> x % 5 == 0, "Buzz");
}}
)
);
}
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 | package com.github.curriculeon; |
2 | |
3 | import java.util.*; |
4 | import java.util.function.Predicate; |
5 | import java.util.stream.Collectors; |
6 | import java.util.stream.IntStream; |
7 | |
8 | |
9 | // And https://github.com/Git-Leon personal memory optimizations of above. |
10 | // https://github.com/Git-Leon/FizzBuzz/blob/master/src/main/java/com/github/curriculeon/Main.java |
11 | |
12 | public class Main { |
13 | public static void main(String[] args) { |
14 | System.out.println( |
15 | fizzBuzz( |
16 | IntStream.rangeClosed(1, 100).toArray(), |
17 | new LinkedHashMap<Predicate<Integer>, String>() {{ |
18 | put(x1 -> x1 % 3 == 0, "Fizz"); |
19 | put(x -> x % 5 == 0, "Buzz"); |
20 | }} |
21 | ) |
22 | ); |
23 | } |
24 | |
25 | private static String fizzBuzz(int[] arr, Map<Predicate<Integer>, String> preds) { |
26 | return Arrays.stream(arr) |
27 | .mapToObj(x -> preds.entrySet().stream() |
28 | .filter(entry -> entry.getKey().test(x)) |
29 | .map(Map.Entry::getValue) |
30 | .reduce(String::concat) |
31 | .orElse(String.valueOf(x))) |
32 | .collect(Collectors.joining(" ")); |
33 | } |
34 | } |