forked from Rapter1990/JavaStreamAPIExamples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFunctionExample.java
More file actions
39 lines (25 loc) · 1.19 KB
/
FunctionExample.java
File metadata and controls
39 lines (25 loc) · 1.19 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
package functionalexamples;
import java.util.Locale;
import java.util.function.Function;
public class FunctionExample {
static Function<String,String> upperCase = (name) -> name.toUpperCase();
static Function<String,String> addSomeString = (name) -> name.toUpperCase().concat("Something");
static Function<String,Integer> strLength = (name) -> name.length();
public static void main(String[] args) {
Locale.setDefault(Locale.forLanguageTag("en-US"));
System.out.println("Result is : " + upperCase.apply("Stream"));
System.out.println("Result of andthen : " + upperCase.andThen(addSomeString).apply("Stream"));
System.out.println("Result of compose : " + upperCase.compose(addSomeString).apply("Stream"));
System.out.println("Result of strLength : " + strLength.apply("Stream"));
// Always returns its input argument
Function<String,String> abc = Function.identity();
System.out.println(abc.apply("Stream"));
/*
Result is : STREAM
Result of andthen : STREAMSomething
Result of compose : STREAMSOMETHING
Result of strLength : 6
Stream
*/
}
}