(parameters) -> expression
or
(parameters) -> { statements; }
1. (int x, int y) -> x + y // takes two integers and returns their sum
2. (x, y) -> x - y // takes two numbers and returns their difference
3. () -> 42 // takes no values and returns 42
4. (String s) -> System.out.println(s)
// takes a string, prints its value to the console, and returns nothing
5. x -> 2 * x // takes a number and returns the result of doubling it
6. c -> { int s = c.size(); c.clear(); return s; } // takes a collection, clears it, and returns its previous size
Functional interfaces have a single functionality to exhibit. For example, a Comparable interface with a single method 'compareTo' is used for comparison purpose. Java 8 has defined a lot of functional interfaces to be used extensively in lambda expressions.
IntOperation is a functional interface since it contain single method.
interface IntOperation { int operate(int i); }
IntOperation iop = x -> x * 2;
System.out.println(iop.operate(5));
class Bar { int i; Foo foo = i -> i * 2; }; //member variable - leagal
void bar() { int i; Foo foo = i -> i * 2; }; // local variable Illegal: variable i is already defined
Runnable r = () -> {System.out.println("Hello World");};
new Thread(r).start();
Referance : (::)
1. static method referance (Type :: Method Name)
2.instant method referance (Type :: Method Name)
2. construtor method referance (Type :: New)
Method references are handles to such existing methods.
String::valueOf
Integer::compare
ReferenceType::Identifier
Constructor references are created using syntax similar to that for method references, but with the method name replaced with the keyword new
List
List
System.out.println(intList);
Default Methods ::
declare concrete methods in the form of default implementations.
public interface Sized {
public default boolean isEmpty() {
return size() == 0;
}
public int size();
}
most specific default-providing interface is selected.
interface A{ public default boolean isEmpty() { return true; } }
interface B{ public default boolean isEmpty() { return true; } }
public class HelloWorld implements A,B-> //Compilation Error
interface A { default void hello() { System.out.println("Hello World from A"); }}
interface B extends A { default void hello() { System.out.println("Hello World from B"); } //Override Here - since its most specific }
public class HelloWorld implements B,A{ No Compilation error // B will be printed
Diamond problem // - No Compilation Error
interface A { default void m() {} ;}
interface B extends A {}
interface C extends A {}
public class HelloWorld implements B,C{
interface A { default void m() {} ;}
interface B extends A { default void m() {} ; }
interface C extends A { default void m() {} ; }
public class HelloWorld implements B,C{ // Compilation Error
interface A { default void m() {} ;}
interface B extends A { }
interface C extends A { default void m() {} ; }
public class HelloWorld implements B,C{ //No Compilation Error
interface A { default void m() {} ;}
interface B extends A { default void m() {} ; }
interface C extends A { }
public class HelloWorld implements B,C{ // No - Compilation Error
interface A {default void m() { System.out.println("hello from A"); } }
IntStream.range(0, 5(Count)).forEach(i -> A.m()); // 5 times m method will be printed
void repeat(int count, Runnable action) {
IntStream.range(0, count).forEach(i -> action.run());
}
repeat(3, () -> System.out.println("Hello!"));
List
iterator.forEachRemaining(list::add);
A stream is a sequence of values. The package java.util.stream
filter Predicate
map Function
sorted Comparator
limit, skip Stream
sample eager/terminal operations
reduce BinaryOperator
findFirst Predicate
forEach Consumer
No comments:
Post a Comment