Functional Java: How to do pattern matching in Java 8?

 1 year ago
source link: https://blog.knoldus.com/functional-java-how-to-do-pattern-matching-in-java/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Functional Java: How to do pattern matching in Java 8?

Reading Time: 2 minutes

In this article, we would learn how we can do pattern matching in Java 8. We have seen pattern matching in Scala so far and now you must be wondering how is that possible in Java as It was never introduced, this is very much possible with Vavr library.

Pattern matching has been one feature which is often used while programming in Scala and ever wondered if that would be available sometime in Java, I am sure every Java developer has wondered that if he or she was a Scala developer before. Also, this is a functional way of doing things and do match on the object as per the requirement.

Now, let’s first understand What is pattern matching?

Pattern matching is a mechanism for checking a value against a pattern. A successful match can also deconstruct a value into its constituent parts. It is a more powerful version of the switch statement in Java and it can likewise be used in place of a series of if/else statements.

In pattern matching match is essentially an expresssion which yields some result. Also, pattern matching is really a great feature to save a lot of time as it avoids to write if else branches and the code looks more clean and readable.

Vavr library provides a match API which is pretty much similar to Scala’s pattern matching.

Why pattern matching is required?

Pattern matching is essentially a functional way of writing the code. Also, it avoids using if and else branches and can dramatically make the code look concise and readable.

How can we implement pattern matching in Java 8?
Of course, this is not possible in Java as of now. However, libraries like Vavr and Cyclops made it possible to have pattern matching in Java.

To give you a quick code walk thorugh of pattern matching. Let’s first see How pattern matching is done in Scala?

Scala pattern matching

import scala.util.Random

val x: Int = Random.nextInt(10)

x match {

case 0 => "zero"

case 1 => "one"

case 2 => "two"

case _ => "other"


Now, let’s see pattern matching in Java using Vavr library
Pattern matching using Vavr.

String number = Match(2).of(





where Case is keyword. Also, we can put a conditional pattern like below

//conditional pattern

String conditional = Match(1).of(

Case($(is(1)),i -> "one" +i),

Case($(is(2)),i -> "two" + i),



Pattern match on object

//with object
User user = new User(1, "Deepak");

User result = Match(user).of(

Case($((new User(1, "Deepak"))), student -> student),

Case($(), new User(0, "default")));

System.out.println(result); // User{id=1, name='Deepak'}

If I want a match against an object that can be done using the above example.
That’s pretty much it for pattern matching in Java using Vavr library, you can also checkout the github repository for code examples here.

Also, you can write in your feedback and queries if you have. I will keep posting articles like this and if you are interested, please do follow us.
Keep reading and smiling.

About Joyk

Aggregate valuable and interesting links.
Joyk means Joy of geeK