Java Login

About javalogin.com

Hello guys,
javalogin.com is for Java and J2EE developers, all examples are simple and easy to understand 

It is developed and maintained by Vaibhav Sharma. The views expressed on this website are his own and do not necessarily reflect the views of his former, current or future employers. I am professional Web development. I work for an IT company as Senior Consultant. Primary I write about spring, hibernate and web-services. I am trying to present here new technologies.


     << Previous
Next >>     


Functional Interface


In Java 8 a functional interface is defined as an interface with exactly one abstract method. @FunctionalInterface annotation is useful for compilation time checking of your code. You cannot have more than one method besides static, default and abstract methods that override methods in Object in your @FunctionalInterface or any other interface used as a functional interface.
But you can use lambdas without this annotation as well as you can override methods without @Override annotation.
This even applies to interfaces that were created with previous versions of Java. Java 8 comes with several new functional interfaces in the package, java.util.function.

  • Function<T,R> - takes an object of type T and returns R.
  • Supplier<T> - just returns an object of type T.
  • Predicate<T> - returns a boolean value based on input of type T.
  • Consumer<T> - performs an action with given object of type T.
  • BiFunction - like Function but with two parameters.
  • BiConsumer - like Consumer but with two parameters.

It also comes with several corresponding interfaces for primitive types, such as:

  • IntConsumer
  • IntFunction<R>
  • IntPredicate
  • IntSupplier

The coolest thing about functional interfaces is that they can be assigned to anything that would fulfill their contract. Take the following code for example:

  • Function<String, String> atr = (name) -> {return "@" + name;};
  • Function<String, Integer> leng = (name) -> name.length();
  • Function<String, Integer> leng2 = String::length;

This code is perfectly valid Java 8. The first line defines a function that prepends "@" to a String. The last two lines define functions that do the same thing: get the length of a String. The Java compiler is smart enough to convert the method reference to String's length() method into a Function (a functional interface) whose apply method takes a String and returns an Integer. For example:


for (String s : args) out.println(leng2.apply(s));


This would print out the lengths of the given strings.


FunctionalInterfaceExample.java

package java8;

public class FunctionalInterfaceExample {

public static void main(String[] args) {
ExampleFunctionalInterface obj = (msg) -> {
System.out.println(msg);
};
obj.printEvenOdd("Hello World");
}

}

@FunctionalInterface
interface ExampleFunctionalInterface {
public void printEvenOdd(String msg);
}

Any interface can be functional interface, not merely those that come with Java. To declare your intention that an interface is functional, use the @FunctionalInterface annotation. Although not necessary, it will cause a compilation error if your interface does not satisfy the requirements (ie. one abstract method).




     << Previous
Next >>