Java8 Fizz Buzz - No Fluff Just Stuff

Java8 Fizz Buzz

Posted by: Demian Neidetcher on January 17, 2014

Here's my attempt at FizzBuzz in Java8 with Lambdas. This will not go in the Pantheon of awesome code and I'm sure I'll tweak it as I learn more. Oh and this is a self-contained implementation and unit test, just messing around.

Here are some thoughts:

  • Not sure if there's a nicer way to do the predicates inline with the Stream API.
  • I wish if statements were expressions, that would clean up some of the code.
  • It'd be neato if FizzBuzz was conducive to using function composition to do a divBy15 but it isn't needed for FizzBuzz.
  • Ranges are nice to see.
package javafunctional.java8.examples;

import org.junit.Test;

import java.util.function.Predicate;
import java.util.stream.IntStream;

import static org.junit.Assert.assertEquals;

public class FunctionLiterals {
    private Predicate<Integer> divBy3() { return arg -> (arg % 3) == 0;}
    private Predicate<Integer> divBy5() { return arg -> (arg % 5) == 0;}

    @Test public void testPredicates(){
        assertEquals(true, divBy3().test(3));
        assertEquals(true, divBy5().test(5));
    }

    public String fizzBuzz(Integer intIn){
        String fizz = (divBy3().test(intIn))? "Fizz" : "";
        String buzz = (divBy5().test(intIn))? "Buzz" : "";
        return (fizz.isEmpty() && buzz.isEmpty())? intIn.toString() : fizz + buzz;
    }

    @Test public void testFizzBuzz() {
        assertEquals("Fizz", fizzBuzz(9));
        assertEquals("Buzz", fizzBuzz(10));
        assertEquals("FizzBuzz", fizzBuzz(30));
    }

    @Test public void runit(){
        IntStream.range(1, 100)
                .mapToObj(ii -> fizzBuzz(ii))
                .forEach(s -> System.out.println(s));
    }
}
Demian Neidetcher

About Demian Neidetcher

Demian Neidetcher is a Senior Engineer at Time Warner Cable working on customer portals and getting television content to IP devices. He first got the programming bug staying up late nights with his Commodore64.

He has been professionally writing software for over 15 years. Most of his experience is with JVM languages (Java, Scala, Groovy) in the telecommunications domain doing things like inventory systems for a long-haul carrier, integrating conferencing software and routing VoIP traffic including geo-spatial 911 call routing. He has worked for companies ranging from Fortune 500 to small start-ups. In every environment Demian has looked for pragmatic approaches, solutions and process to get teams delivering software that benefits users.

Why Attend the NFJS Tour?

  • » Cutting-Edge Technologies
  • » Agile Practices
  • » Peer Exchange

Current Topics:

  • Languages on the JVM: Scala, Groovy, Clojure
  • Enterprise Java
  • Core Java, Java 8
  • Agility
  • Testing: Geb, Spock, Easyb
  • REST
  • NoSQL: MongoDB, Cassandra
  • Hadoop
  • Spring 4
  • Cloud
  • Automation Tools: Gradle, Git, Jenkins, Sonar
  • HTML5, CSS3, AngularJS, jQuery, Usability
  • Mobile Apps - iPhone and Android
  • More...
Learn More »