Last active
July 28, 2017 02:02
-
-
Save sis-yoshiday/3abaeb5a59282c86cd910a53dc13af2d to your computer and use it in GitHub Desktop.
SemicolonLessFizzBuzz
This file contains 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; | |
import java.util.stream.Stream; | |
public class SemicolonLessFizzBuzz { | |
public static void main(String[] args) { | |
System.out.println("### 1 ###\n"); | |
case1(); | |
System.out.println(); | |
System.out.println("### 2 ###\n"); | |
case2(); | |
System.out.println(); | |
System.out.println("### 3 ###\n"); | |
case3(); | |
System.out.println(); | |
} | |
private static void case1() { | |
if (IntStream.rangeClosed(1, 100) | |
.boxed() | |
.map(i -> | |
i % 15 == 0 | |
? "Fizz Buzz" | |
: i % 3 == 0 | |
? "Fizz" | |
: i % 5 == 0 | |
? "Buzz" | |
: String.valueOf(i)) | |
.peek(System.out::println) | |
.count() > 0) { | |
} | |
} | |
private static void case2() { | |
try (Stream<String> s = IntStream.rangeClosed(1, 100) | |
.boxed() | |
.map(i -> | |
i % 15 == 0 | |
? "Fizz Buzz" | |
: i % 3 == 0 | |
? "Fizz" | |
: i % 5 == 0 | |
? "Buzz" | |
: String.valueOf(i)) | |
.peek(System.out::println)) { | |
if (!s.collect(Collectors.toSet()).isEmpty()) {} | |
} | |
} | |
private static void case3() { | |
try (Stream<String> s = IntStream.rangeClosed(1, 100) | |
.boxed() | |
.map(i -> | |
i % 15 == 0 | |
? "Fizz Buzz" | |
: i % 3 == 0 | |
? "Fizz" | |
: i % 5 == 0 | |
? "Buzz" | |
: String.valueOf(i))) { | |
if (Stream.of(s.collect(Collectors.joining("\n"))).peek(System.out::print).count() > 0) {} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment