Skip to content

Instantly share code, notes, and snippets.

@cchacin
Last active April 11, 2020 20:51
Show Gist options
  • Save cchacin/9926141 to your computer and use it in GitHub Desktop.
Save cchacin/9926141 to your computer and use it in GitHub Desktop.
Java 8 FizzBuzz
public class FizzBuzz {
static java.util.function.Predicate<Integer> divBy(int n) { return i -> (i % n) == 0;}
static String fizzBuzz(Integer intIn){
String fizz = divBy(3).test(intIn) ? "Fizz" : "";
String buzz = divBy(5).test(intIn) ? "Buzz" : "";
return (fizz.isEmpty() && buzz.isEmpty()) ? intIn.toString() : fizz + buzz;
}
public static void main(String[] args) {
int start = Integer.parseInt(args[0]);
int end = Integer.parseInt(args[1]);
java.util.stream.IntStream
.rangeClosed(start, end)
.mapToObj(Java8Carlos::fizzBuzz)
.forEach(System.out::println);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment