Welcome to

Evaluate Expression

Home / Java / Java 8 functional interfaces

Java 8 functional interfaces

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.

Project Euler problem #3

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:

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

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

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”
  1. Business-Flows – Evaluate Expression January 20, 2017on4:06 am Reply

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

Leave a Reply

Your email address will not be published. Required fields are marked *

Thanks for reading!

>> <<