Java 8 Notes

 Reference link: youtube.com/watch?v=oNdMigiwzlU&t=227s- java 8 programming questions https://www.youtube.com/watchv=QRwG9X9l6xI&list=PLsyeobzWxl7qbvNnJKjYbkTLn2w3eRy1Q- 

-----------what is lamda expression/how to write lamda expression


exampl1:


()->{SOPL("Hello")}--curly braces is optional when one line is written


example 2:


(a,b)->{sopl(a+b)}---compiler can guess the data type


example: 3


n->n*n--when only one input parameter then parenthesis is optional, and return is not required if you are not using curly braces.


example : 4


s-> s.length;


-----------------------calling the lamda function/Functional Interface--------------------------


what is lamda expression : lamda expression is nothing but anonymous function

-no name

- no modifier

- no return type

- special arrow symbol

how to write lamda expression 

what is the functional interface


n->return n*n; ---invalid

n->{return n*n;};----valid

n-> {return n*n};----invalid

n->{n*n;};------invalid

n->n*n;--valid


Note: 


1. without curly baraces, we can't use return keyword. compiler will consider returned value automatically.

2. within curly braces if we want to return some value compulsory we should use return statement.


@FunctionalInterface annotation- specify explicitly, this is functional interface


Define functional interface: An interface which contains single abstract method is called fuctional interface


Understanding functional interface in case of inheritance


functional interface and lamda expression connection: instead of having a implementation class of a functional interface, we can directly use lamda expression to 

provide the implementation of the interface method or call the interface method. Functional interface is not generalized thing, its a specific thing, 

wherever there is functional interface we can use lamda expression.


example: 

@FunctionalInterface

Interface interf{


public void m1();


}



class Test{


public static void main(String[] args){


Interf i = ()-> System.out.println(" Hello");

}


}

 



No class will be generated for lamda expression.


write a program to sort a arraylist using lamda expression of comparator implementation.

write a program to find the second highest salary employ salary based on employee id


-----------------Anonymous class vs lamda expression


-anonymous class is not equal to lamda expresssion

-if anonymous class imlements an interface which contains single abstract method then only we can replace that anonymous class with lamda expression


---------------------Default method() & Static method() inside Interface:


-default method is also called defender method or virtual extension method

- signature of default method: default void m3(){};, default is not modifier here,since the method has default implementation that's why method name is default

- implementation class dont need to provide implementation of this method

- without affeccting implementation classes if we want to add new method to the interface then we can go for default method

- overriding default method of interface in a class. We need to remove default word while overriding in the implmentation class, after overriding in implementation class

it will be public void m3(){}.



- static method of interface is not by default available to the implementation class

- static method of interface should be called by interface name only example: Interf.m1(); where m1 is static method of interface Interf.

- static method signature: public static void m1() {} in interface Interf.

- even if a class doesn't implement interface, class can call the static method of interface by Interf.m1();

- main method can also be declared in interface from java 1.8 onwards, and you can run main method directly from command prompt example: java Interf.

- why interface static method came: to define general utility method we can go for this method rather than defining in a class, class is costly 


--------------------Predicate: Predefined functional interface


-predefined functional interface:

Predicate

Function

Consumer

Supplier


-two argument functional interface

BiPredicate

BiFunction

BiConsumer


-Primitive function interfaces

IntPredicate

IntFunction

IntConsumer


to be continued....



-------------------------------Predicate---------------------------------------------- 

Take some input and perform some conditional check and return boolean value. 

It contains test() method, it is used for conditional checks. 

It is predifined functional interface. The return type is always boolean.It's boolean valued function.



interface Predicate<T>{


public boolean test<T t>();


}

--preedicate example in method

public boolean test (Integer i){


if (i%2==0){


return true;

}


else{


return false;

}


}


or 


(Integer i)->I%2==0


or 


I->I%2==0


example:


import java.util.function.*;

class Test{


public static void main(String [] args){

Predicate<Integer> p1 = i->i%2==0;

Syso(p1.test(10));

}


}


example 2: checking if the length of string is greater than 5 or not


import java.util.function.*;

class Test{


public static void main(String [] args){


String [] s = {"Nag", "Chiranjeevi", "Venkatesh", "Balaiah","Sunny","Katrina"};

Predicate<String> p = s1->s.length()>5;

for (String s1: s){


if (p.test(s1)){


syso(s1)


}


}

}


}


example 3:


public static void main(String [] args){


// adding employee object in array list


// checking employee object if their salary is greater than 3000 or not


Predicate <Employee> p = e-> e.salary>3000; // you can use 10 condition in one line only


for (Employee e1 : list){


if (p.test(e1)){syso(e1.name ":"+e1.salary);}


}


}


predicate joining:


and () method


p1.and(p2).test(10);


or () method


p1.or(p2).test(10);


negate() method


p1.negate() // opposite of p1


-----------------------------Function- prededined functional interface


- It takes some input and perform some operation and return the result which need not be boolean.


interface Function <T, R>{


public R apply (T t);

}


example 1: 


import java.util.function.*;

class Test{


public static void main(String [] args){



Function<Integer, Integer> f = i->i*i;

syso(f.apply(4)); 

} }



example 2:



import java.util.function.*;

class Test{


public static void main(String [] args){



Function<String, Integer> f = s->s.length();

syso(f.apply("Vikash")); 

} }


example 3:



import java.util.function.*;

class Test{


public static void main(String [] args){



Function<String, String> f = s->s.toUpperCase();

syso(f.apply("durgasoftwaresolutions")); 

} }


example 4: // apply function for Student object to find grade



import java.util.function.*;

class Test{


public static void main(String [] args){



Function<Student, String> f = s->{


int marks = s.marks;

String grade="";

if (marks>=80) grade ="A";

else if(marks>=60) grade="B";

else if(marks>=50) grade="C";

else if(marks>=35) grade="D";

else  grade="F";

};


Student [] s ={

new Student("Durga",100); // define studnet class separately with name, marks and grade variable

new Student("Sunny",65);

new Student("Bunny",55);

new Student("Chinny",45);

new Student("Vinny",25);

};


for (Student s1: s){


syso("student name:"+ s1.name);

syso("student marks:"+ s1.marks);

syso("student grade:"+ f.apply(s1));


}

} }



example 5: use of predicate with function


Functiona chaining:


f1.andThen(f2).apply(i);// first f1 then f2

f1.andThen(f2).andThen(f3).apply(i);//first f1 then f2 then f3

f1.compose(f2).apply(i);// first f2 then f1


---------------------------------------CONSUMER


-It accepts some input and perform required operation and not required to return anything. 


Predicate<T>----------------->return boolean value

Function<T,R>------------------>return something

Consumer<T>-------------------> doesn't return anything


T*--Input type

R* ---Return type


consumer example 1: 


import java.util.function.*;

class Test{


public static void main(String [] args){



Consumer<String> c = s->syso(s);

c.accept("durga");

} }


--------------------------------------supplier


- Just supply my required object and it won't take any input is called supplier


interface Supplier<R>{


public R get();


}



example 1:



import java.util.function.*;

import java.util.Date;

class Test{


public static void main(String [] args){



Supplier<Date> s = ()->new Date();

syso(s.get());

} }

Comments