039 Functions Study Material 2
039 Functions Study Material 2
Features in
Simple Way
Predicate
Study Material
1 https://www.youtube.com/durgasoftware
Java 8 New
Features in
Simple Way
Functions
Functions are exactly same as predicates except that functions can return any type of result
but function should (can) return only one value and that value can be any type as per our
requirement.
To implement functions oracle people introduced Function interface in 1.8version.
Function interface present in Java.util.function package.
Functional interface contains only one method i.e., apply()
1) interface function(T,R) {
2) public R apply(T t);
3) }
1) import Java.util.function.*;
2) class Test {
3) public static void main(String[] args) {
4) Function<String, Integer> f = s ->s.length();
5) System.out.println(f.apply("Durga"));
6) System.out.println(f.apply("Soft"));
7) }
8) }
Note: Function is a functional interface and hence it can refer lambda expression.
2 https://www.youtube.com/durgasoftware
Java 8 New
Features in
Simple Way
Predicate can return only Function can return any type of value
boolean value.
Note: Predicate is a boolean valued function and(), or(), negate() are default methods present
inside Predicate interface.
3 https://www.youtube.com/durgasoftware
Java 8 New
Features in
Simple Way
Output: durgasoftwaresolutionshyderabad
Output: 3
4 https://www.youtube.com/durgasoftware
Java 8 New
Features in
Simple Way
5 https://www.youtube.com/durgasoftware
Java 8 New
Features in
Simple Way
Output:
D:\durgaclasses>java Test
Student Name:Sunny
Student Marks:100
Student Grade:A[Dictinction]
Student Name:Bunny
Student Marks:65
Student Grade:B[First Class]
Student Name:Chinny
Student Marks:55
Student Grade:C[Second Class]
Student Name:Vinny
Student Marks:45
Student Grade:D[Third Class]
Student Name:Pinny
Student Marks:25
Student Grade:E[Failed]
6 https://www.youtube.com/durgasoftware
Java 8 New
Features in
Simple Way
22) {
23) return "A[Dictinction]";
24) }
25) else if(marks>=60)
26) {
27) return "B[First Class]";
28) }
29) else if(marks>=50)
30) {
31) return "C[Second Class]";
32) }
33) else if(marks>=35)
34) {
35) return "D[Third Class]";
36) }
37) else
38) {
39) return "E[Failed]";
40) }
41) };
42) Predicate<Student> p=s->s.marks>=60;
43)
44) for(Student s : l)
45) {
46) if(p.test(s))
47) {
48) System.out.println("Student Name:"+s.name);
49) System.out.println("Student Marks:"+s.marks);
50) System.out.println("Student Grade:"+f.apply(s));
51) System.out.println();
52) }
53) }
54) }
55) public static void populate(ArrayList<Student> l)
56) {
57) l.add(new Student("Sunny",100));
58) l.add(new Student("Bunny",65));
59) l.add(new Student("Chinny",55));
60) l.add(new Student("Vinny",45));
61) l.add(new Student("Pinny",25));
62) }
63) }
Output:
D:\durgaclasses>java Test
Student Name:Sunny
7 https://www.youtube.com/durgasoftware
Java 8 New
Features in
Simple Way
Student Marks:100
Student Grade:A[Dictinction]
Student Name:Bunny
Student Marks:65
Student Grade:B[First Class]
8 https://www.youtube.com/durgasoftware
Java 8 New
Features in
Simple Way
Output:
D:\durgaclasses>java Test
[Sunny:1000.0, Bunny:2000.0, Chinny:3000.0, Pinny:4000.0, Vinny:5000.0]
The total salary of this month:15000.0
9 https://www.youtube.com/durgasoftware
Java 8 New
Features in
Simple Way
Output:
Before Increment:
[Sunny:1000.0, Bunny:2000.0, Chinny:3000.0, Pinny:4000.0, Vinny:5000.0, Durga:10000.0]
After Increment:
[Sunny:1477.0, Bunny:2477.0, Chinny:3477.0, Pinny:4000.0, Vinny:5000.0, Durga:10000.0]
Employees with incremented salary:
[Sunny:1477.0, Bunny:2477.0, Chinny:3477.0]
10 https://www.youtube.com/durgasoftware
Java 8 New
Features in
Simple Way
Function Chaining:
We can combine multiple functions together to form more complex functions.For this Function
interface defines the following 2 default methods:
f1.andThen(f2): First f1 will be applied and then for the result f2 will be applied.
f1.compose(f2)===>First f2 will be applied and then for the result f1 will be applied.
Output:
The Result of f1:AISHWARYAABHI
The Result of f2:Aishwarya
The Result of f1.andThen(f2):AISHWARYA
The Result of f1.compose(f2):AISHWARYA
11 https://www.youtube.com/durgasoftware
Java 8 New
Features in
Simple Way
9) System.out.println(f1.compose(f2).apply(2));
10) }
11) }
Output:
64
16
Output:
D:\durgaclasses>java Test
Enter User Name:
durga
Enter Password:
java
Valid User
D:\durgaclasses>java Test
12 https://www.youtube.com/durgasoftware
Java 8 New
Features in
Simple Way
D:\durgaclasses>java Test
Enter User Name:
DURGATECHNOLOGIES
Enter Password:
java
Valid User
D:\durgaclasses>java Test
Enter User Name:
javajava
Enter Password:
java
Invalid User
D:\durgaclasses>java Test
Enter User Name:
durga
Enter Password:
Java
Invalid User
Eg:
1) import java.util.function.*;
2) class Test
3) {
4) public static void main(String[] args)
5) {
6) Function<String,String> f1= Function.identity();
7) String s2= f1.apply("durga");
8) System.out.println(s2);
9) }
10) }
Output: durga
13 https://www.youtube.com/durgasoftware