- Function Interface
- Java.util.function has special interface like bleow, it contains generic methods used as type for lambda expression with same type and signature.
- An interface with exactly one abstract method is called Functional Interface.
- @FunctionalInterface annotation is added so that we can mark an interface as functional interface.
- If we try to have more than one abstract method, it throws compiler error.
- The major benefit of java 8 functional interfaces is that we can use lambda expressions to instantiate them and avoid using bulky anonymous class implementation.
- Java 8 has defined a lot of functional interfaces in java.util.function package. Some of the useful java 8 functional interfaces are Runnable, Consumer, Supplier, Function and Predicate
- Custom
@FunctionalInterface public interface Square { public int calculate(int number); } // Custom Square sq = (i) -> (i * i); int result = sq.calculate(2); System.out.println(result);
- Runnable
Thread t = new Thread(() -> { System.out.println("Thread Running"); }); t.start();
- Predecate Interface
- The Functional Interface PREDICATE is defined in the java.util.Function package.It improves manageability of code, helps in unit-testing them separately, and contain some methods like:
- isEqual(Object targetRef) : Returns a predicate that tests if two arguments are equal according to Objects.equals(Object, Object).
- and(Predicate other) : Returns a composed predicate that represents a short-circuiting logical AND of this predicate and another.
- negate() : Returns a predicate that represents the logical negation of this predicate.
- or(Predicate other) : Returns a composed predicate that represents a short-circuiting logical OR of this predicate and another.
- test(T t) : Evaluates this predicate on the given argument.boolean test(T t)
// Simple Predicate Predicate
lesserThan = (i) -> (i > 10); System.out.println(lesserThan.test(11)); // Predicate Chaining Predicate lessThan = (i) -> (i < 10); Predicate greaterThan = (i) -> (i > 5); boolean result = lesserThan.and(greaterThan).test(6); System.out.println(result); // negation boolean result2 = lesserThan.and(greaterThan).negate().test(6); System.out.println(result2); Predicate equals = (i) -> (i == 2); boolean result3 = equals.test(2); System.out.println(result3); - Static methods and Default methods
- Default methods
- Java 8 interface changes include static methods and default methods in interfaces. Prior to Java 8, we could have only method declarations in the interfaces. But from Java 8, we can have default methods and static methods in the interfaces.
- For creating a default method in java interface, we need to use “default” keyword with the method signature. For example,
public interface Interface2 { void method2(); default void log(String str){ System.out.println("I2 logging::"+str); } }
- Static methods
- Java interface static method is similar to default method except that we can’t override them in the implementation classes.
public interface MyData { default void print(String str) { if (!isNull(str)) System.out.println("MyData Print::" + str); } static boolean isNull(String str) { System.out.println("Interface Null Check"); return str == null ? true : "".equals(str) ? true : false; } }
- Java interface static methods are good for providing utility methods, for example null check, collection sorting etc.
- Abstract Class Needs
- Abstract class can define constructor. They are more structured and can have a state associated with them.
- The constraint on the default method is that it can be implemented only in the terms of calls to other interface methods, with no reference to a particular implementation's state. So the main use case is higher-level and convenience methods.
Thursday, 29 November 2018
Java 8 New
About Unknown
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment