concept pattern matching in category java

This is an excerpt from Manning's book Modern Java in Action: Lambdas, streams, reactive and functional programming.
In Java, you would write an if-then-else or a switch statement. Other languages have shown that, for more complex data types, pattern matching can express programming ideas more concisely compared to using if-then-else. For such data types, you might also use polymorphism and method overriding as an alternative to if-then-else, but there’s ongoing language-design discussion as to which is more appropriate.[9] We’d say that both are useful tools and that you should have both in your armory. Unfortunately, Java 8 doesn’t have full support for pattern matching, although we show how it can be expressed in chapter 19. A Java Enhancement Proposal is also being discussed to support pattern matching in a future version of Java (see http://openjdk.java.net/jeps/305). In the meantime, let’s illustrate with an example expressed in the Scala programming language (another Java-like language using the JVM that has inspired some aspects of Java evolution; see chapter 20). Suppose you want to write a program that does basic simplifications on a tree representing an arithmetic expression. Given a data type Expr representing such expressions, in Scala you can write the following code to decompose an Expr into its parts and then return another Expr:
There’s one other important aspect to what’s generally regarded as functional programming: (structural) pattern matching, which isn’t to be confused with pattern matching and regex. Chapter 1 ended by observing that mathematics can write definitions such as
f(0) = 1 f(n) = n*f(n-1) otherwise
Pattern matching is a functional feature that lets you unwrap data types. You can view data matching as generalizing Java’s switch statement.

This is an excerpt from Manning's book Java 8 in Action: Lambdas, streams, and functional-style programming.
In Java you would here write an if-then-else or a switch statement. Other languages have shown that, for more complex data types, pattern matching can express programming ideas more concisely compared to using if-then-else. For such data types, you might also use polymorphism and method overriding as an alternative to if-then-else, but there’s still language-design discussion as to which is more appropriate.[9] We’d say that both are useful tools and you should have both in your armory. Unfortunately, Java 8 doesn’t have full support for pattern matching, although we show how it can be expressed in chapter 14. In the meantime, we illustrate with an example expressed in the Scala programming language (another Java-like language using the JVM that has inspired some aspects of Java evolution; see chapter 15). Suppose you want to write a program that does basic simplifications on a tree representing an arithmetic expression. Given a data type Expr representing such expressions, in Scala you can write the following code to decompose an Expr into its parts and then return another Expr:
Pattern matching is a functional feature that lets you unwrap data types. It can be seen as generalizing Java’s switch statement.
There’s a simpler solution using a feature called pattern matching. It’s not available in Java, so we’re going to use small examples from the Scala programming language to exemplify pattern matching. It will give you an idea of what could be possible in Java if pattern matching were supported.
Listing 14.1. Implementing pattern matching to simplify an expression
public static Expr simplify(Expr e) { TriFunction<String, Expr, Expr, Expr> binopcase = #1 (opname, left, right) -> { if ("+".equals(opname)) { #2 if (left instanceof Number && ((Number) left).val == 0) { return right; } if (right instanceof Number && ((Number) right).val == 0) { return left; } } if ("*".equals(opname)) { #3 if (left instanceof Number && ((Number) left).val == 1) { return right; } if (right instanceof Number && ((Number) right).val == 1) { return left; } } return new BinOp(opname, left, right); }; Function<Integer, Expr> numcase = val -> new Number(val); #4 Supplier<Expr> defaultcase = () -> new Number(0); #5 return patternMatchExpr(e, binopcase, numcase, defaultcase); #6 } #1: Deals with a BinOp expression #2: Deals with the addition case #3: Deals with the multiplication case #4: Deals with a Number #5: A default case if the user provides an Expr that’s not recognized #6: Applies pattern matching