Created
July 31, 2014 07:40
-
-
Save rpomeroy/0160ae7f525a4caf4ecb to your computer and use it in GitHub Desktop.
FizzBuzz using Java 8 Functional approach
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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