-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFunctionExample.java
More file actions
41 lines (33 loc) · 1.11 KB
/
FunctionExample.java
File metadata and controls
41 lines (33 loc) · 1.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
package predefined_functional_interfaces.function;
/*
* Example demonstrating use of Function.
*/
import java.util.function.Function;
import java.util.function.Predicate;
public class FunctionExample {
public static void main(String[] args) {
Function<Integer,Double> complexFormula = i -> Math.sqrt(i*100);
System.out.println(complexFormula.apply(20));
System.out.println("==============");
Predicate<String> startsWithA = s -> s.startsWith("A");
Function<String, String> greet = s -> "Hello: " + s.toUpperCase();
String []names = {"Anshuman", "Yuvraj", "Amay", "Ram"};
for(String name: names){
if(startsWithA.test(name)){
System.out.println(greet.apply(name));
}
}
System.out.println("==============");
Function<String,String> removeSpaces = s -> s.replaceAll(" ","");
System.out.println(removeSpaces.apply("Hello, have a nice day"));
}
}
/*
* Output:
* 44.721359549995796
* ==============
* Hello: ANSHUMAN
* Hello: AMAY
* ==============
* Hello,haveaniceday
*/