Functional Java?

Okay, so one of the things that I’ve been trying to master is the Java ecosystem as my current role consists of me utilising a lot of tools & technologies from the Java EE ecosystem, ranging from standard technologies such as Hibernate & Spring to bespoke technologies & design patterns.

From working with Java, specifically Hibernate, I’ve discovered a number of things, including just how awesome Streams can be. An example being where you may want to format some result set from a query in Hibernate, of course this can be done in several different ways, but my personal favourite is via the utilisation of the good ol’ map method. Now traditionally in Java, one was typically forced into using a for-each implementation, perhaps creating an empty list then iterating over the result set, adding the relevant data to this list, then returning zed list. Only in my opinion, that’s a little tedious, for such a scenario, in my opinion, it’s much easier to make use of streams.

In my personal opinion, if used correctly, one can make their code much more readable, if not more reliable, of course this would depend on what one is doing, but in such a scenario where you need to check for an array of things, such as if object ‘x’ has ‘z’ property value, and iv object ‘x’ is not null, thanks to methods such as Objects::nonNull, combined with a filter method, it’s easier to read rather than having some large & potentially grotesque if statement in the code.

Now of course, while I love using Streams & the associated syntax(es), they’re not always the best solution in the world, sometimes it is best to stick to a more traditional style. Here’s an example of a comparison to see if a string is contained in a list of strings.

Similar Posts

Notify of
Inline Feedbacks
View all comments