Java 8 Pattern Matching?

I suppose you are not talking about pattern matching in the sense of applying a regular expression on a string, but as applied in Haskell. For instance using wildcards:

head (x:_)  = x
tail (_:xs) = xs

Java 8 will not support that natively, with Lambda expression there are, however, ways to do so, like this for computing the factorial:

public static int fact(int n) {
     return ((Integer) new PatternMatching(
          inCaseOf(0, _ -> 1),
          otherwise(  _ -> n * fact(n - 1))

How to implement that you will find more information in this blog post: Towards Pattern Matching in Java.

Leave a Comment