One of the key elements of the functional programing is that we can treat functions as objects. In a typical functional language we can have a variable with type of function. The benefits are that we can use functions as:

  • • parameter
  • • return type
  • • variable

The type Function in Java is interface with one abstract method. If we want to be sure that an interface can be used as a function type we can add annotation @FunctionalInterface before the header of the interface. It will throw compilation error if the interface contains more then one abstract method.

If a method has a parameter with function type we can use lambda expression instead of creating an anonymous class. Lambda expression represents an anonymous class in a shorter and readable way. Lambda expression consists from parameter definition, small right arrow “->”, and the body of the method.

So instead of this:

new FunctionalInterface() {
    public String method(String a, String b) {
        return “The parameters are ” + a + “ and ” + b;

we can write:

(String a, String b) -> {
    return “The parameters are ” + a + “ and ” + b;

If we have a single line in the body we can skip the braces and the return keyword:

(String a, String b) -> “The parameters are ” + a + “ and ” + b;

The fact that we can have only one abstract method means that we can skip the type of the parameters, like this:

(a, b) -> “The parameters are ” + a + “ and ” + b;

The usage of this feature is very limited in web testing. This feature can be helpful in complex algorithms, but in web testing we mostly just compare the given expected values with the found actual values. Complex calculating with data is rare in a typical test code.

Based on: Java 8 tutorial

Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>