What are functional interfaces?

8

What are functional interfaces in Java?

Has this concept already existed or came out of version 8 of Java?

    
asked by anonymous 31.03.2014 / 21:02

2 answers

12

The interfaces in Java 8 have gained many new and powerful features, such as abstract methods and default .

Speaking specifically about Functional Interfaces , the concept is an interface that contains only one abstract method, such as Runnable ", for example. This type of Interface is used in specific situations, being instantiation is usually through an anonymous class.

Easier setting by annotation

From Java 8, these Interfaces can be annotated with @FunctionalInterface :

@FunctionalInterface
public interface Runnable {
    public abstract void run();
}

When declaring more than one method (except methods already present in the Object class), the compiler will complain, avoiding unnecessary errors.

Where is this used?

Lambda expressions

With the assurance that the Interface has only one method, this concept becomes useful when combined, for example, with a lambda expression .

Suppose we have a method with the signature invoke(Runnable r) . Before Java 8, we could use a new Runnable and override the run() method as follows:

Runnable r = new Runnable() {
    @Override
    public void run() {
        System.out.println("Thread executando...");
    }
};
invoke(r);

In Java 8, the equivalent code is much simpler:

invoke(() -> System.out.println("Running"));

An implementation of Runnable is created, whose content of the single method run() is the value of the lambda expression.

Method references

Java 8 also introduces the concept of method references . This is a great way to reuse code and avoid creating unnecessary classes, especially in the case of classes with one method that serves only to delegate execution to another.

For example, instead of creating a Comparator different each time we need to sort a list or array, we can only pass a reference to the method that makes the comparison. Consider the following implementation:

class ComparisonProvider {
    public int compareByName(Person a, Person b) {
        return a.getName().compareTo(b.getName());
    }

    public int compareByAge(Person a, Person b) {
        return a.getBirthday().compareTo(b.getBirthday());
    }
}

We can reference one of the methods to sort an array, like this:

ComparisonProvider myComparisonProvider = new ComparisonProvider();
Arrays.sort(rosterAsArray, myComparisonProvider::compareByName);

As the second parameter of the sort() method receives a Comparator , which is a Functional Interface, Java can create an implementation for it with the passed method as a reference.

Reading References

31.03.2014 / 22:07
-1

There are also ... Integrated functional interfaces

In addition to the unique abstract method interfaces already mentioned, JDK 8 includes several new functional interfaces. The most common are Function, Predicate, and Consumer, which are defined in the java.util.function package. The map method of Stream uses Function as a parameter. Similarly, filter uses Predicate and forEach uses Consumer. The package also has other functional interfaces like Supplier, BiConsumer and BiFunction.

It is possible to use an integrated functional interface as a parameter for our own methods. For example, consider a Device class with methods such as checkout and checkin to indicate whether a device is in use. When a user requests a new device, the getFromAvailable method returns a set of available devices or creates a new one if needed.

You can implement a feature to borrow a device, as follows:

public void borrowDevice(Consumer<Device> use) {
  Device device = getFromAvailable();

  device.checkout();

  try {
    use.accept(device);      
  } finally {
    device.checkin();
  }
}

Here you will find an excellent reference source ...

link

    
06.03.2018 / 20:10