Skip to content

Instantly share code, notes, and snippets.

@sis-yoshiday
Last active July 28, 2017 02:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sis-yoshiday/3abaeb5a59282c86cd910a53dc13af2d to your computer and use it in GitHub Desktop.
Save sis-yoshiday/3abaeb5a59282c86cd910a53dc13af2d to your computer and use it in GitHub Desktop.
SemicolonLessFizzBuzz
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