- Post By: Thomas Heslin
- Date:
- Category: Java

In Java 8 functional interfaces arrived. Despite being familiar with functional programming, dabbling mainly in F#, I had no experience with these interfaces when it came to use them at work.

Project Euler sprang to mind as a practical way of learning the interfaces. Furthermore I had never test driven these problems before. Here I show part of my solution to problem three. The code is available on GitHub.

The prime factors of 13195 are 5, 7, 13 and 29.

What is the largest prime factor of the number 600851475143 ?

First I solved the problem without using the functional interfaces:

1 2 3 4 5 6 7 |
@Test public void solveProblem() { final long expected = PROBLEM_THREE.getAnswer(); long actual = ProblemThree.largestPrimeFactorOf(600851475143L); assertEquals(expected, actual); } |

Note that I have obscured the answers to the problems on line #3.

1 2 3 4 5 6 7 8 9 10 11 12 13 |
public static long largestPrimeFactorOf(long valueInQuestion) { primeFactors = Collections.emptyList(); final int increment = isOdd(valueInQuestion) ? ODD_INCREMENT : EVEN_INCREMENT; long i = 1; while (i < squareRootOf(valueInQuestion)) { if (isAFactorOf(i, valueInQuestion) && isAPrime(i)) { addToListOfPrimeFactors(i); } i += increment; } |

Now for the functional implementation, the test for which differs only in the method called in ProblemSix.

1 2 3 4 5 |
public static long largestPrimeFactorOfUsingFunctional(long valueInQuestion) { return LongStream.range(1, squareRootOf(valueInQuestion)) .filter(i -> isAFactorOf(i, valueInQuestion) && isAPrime(i)) .max().getAsLong(); } |

Both implementations call *squareRootOf*, *isAFactorOff* and *isAPrime*. I wrote these helper methods whilst writing the regular approach and was able to speed up the functional implementation by calling these functions, which I could rely upon thanks to the unit tests.

The mathematical nature of the Euler problems utilise certain Java 8 interfaces that I will likely not use in industry. Others however, such as *filter*, *iterate*, *findFirst*, *map*, *flatMap*, etc, have and will come into great use.

The future of this small project is to continue to challenge my maths skills, something I’m eager to practice once again after reading Seven Brief Lessons on Physics. An excellent book I’d recommend to anyone interested in physics regardless of ability. The book doesn’t delve into the maths but nonetheless rekindled my enjoyment of the subject.

I would also like to explore the idea of turning this project into a beginner Java tutorial. I envision a separate package which has a set of tests for each problem, likely the tests I have already written, which include boundary tests. The *src* would consist of a class for each Euler problem, the first problem would require the novice to simply add a few lines to get to the solution. The next problem may task them with filling an empty body with a method, named appropriately with a simple return type.

1 thought on “Java 8 functional interfaces”

[…] wrote a blog post a few months ago on this very topic which explores refactoring code into this functional style, […]