DEV Community

Cover image for How to create a boolean arrow function in Java 🏹
Benjamin Rancourt
Benjamin Rancourt

Posted on • Originally published at benjaminrancourt.ca on

How to create a boolean arrow function in Java 🏹

Since I am more experienced with JavaScript than Java, I often search on the Web to find out how to do X in Java when working on Java projects. I guess this is common among people who are able to code in a lot of languages. 😛

Recently, I was just looking to create an arrow function , to add more readability to my Java code. In JavaScript, it's pretty simple:

const isUdeSCategory = (category) => category.displayName === "UdeS";

Enter fullscreen mode Exit fullscreen mode

But, as always, in Java, you have to know the right class to do the same... In this case, after discovering the Predicate class, I was able to quicly adapt my code as I wanted:

final Predicate<OutlookCategory> isUdeSCategory = category -> category?.displayName == "UdeS";

// To keep things simple, I used the equal comparison, but in reality, the function looks like as
// final Predicate<OutlookCategory> predicate = category -> Objects.equals(category.displayName, "UdeS");
// to be null-safe.

Enter fullscreen mode Exit fullscreen mode

If you want to know more about the Predicate interface, JournalDev has a quick article on it named Java Predicate.

I'm pretty sure there are other interfaces for other types of arrow functions in Java, but I guess I'll learn them when I need to! 😉

Top comments (0)

Great read:

Is it Time to go Back to the Monolith?

History repeats itself. Everything old is new again and I’ve been around long enough to see ideas discarded, rediscovered and return triumphantly to overtake the fad. In recent years SQL has made a tremendous comeback from the dead. We love relational databases all over again. I think the Monolith will have its space odyssey moment again. Microservices and serverless are trends pushed by the cloud vendors, designed to sell us more cloud computing resources.

Microservices make very little sense financially for most use cases. Yes, they can ramp down. But when they scale up, they pay the costs in dividends. The increased observability costs alone line the pockets of the “big cloud” vendors.

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay