Skip to content

Instantly share code, notes, and snippets.

@rpomeroy
Created July 31, 2014 07:40
Show Gist options
  • Save rpomeroy/0160ae7f525a4caf4ecb to your computer and use it in GitHub Desktop.
Save rpomeroy/0160ae7f525a4caf4ecb to your computer and use it in GitHub Desktop.
FizzBuzz using Java 8 Functional approach
import java.util.stream.Collectors;
import java.util.stream.IntStream;
public class Main {
public static void main(String[] args) {
System.out.println(IntStream.range(1, 101).boxed()
.map(Main::fizzBuzz)
.collect(Collectors.joining(", ")));
}
public static String fizzBuzz(int val) {
if(val % 15 == 0) return "FizzBuzz";
if(val % 3 == 0) return "Fizz";
if(val % 5 == 0) return "Buzz";
return String.valueOf(val);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment