forked from ymzong/java-precisely
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
46 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package section23; | ||
|
||
import java.util.function.DoubleToIntFunction; | ||
import java.util.function.Function; | ||
import java.util.function.IntUnaryOperator; | ||
|
||
public class FunctionalInterfaces { | ||
|
||
static void functionExamples() { | ||
// All of the following parse a String into Integer | ||
Function<String, Integer> | ||
parse1 = s -> Integer.parseInt(s), // lambda expression | ||
parse2 = (String s) -> Integer.parseInt(s), // lambda expression with explicit typing | ||
parse3 = Integer::parseInt, // method reference to Integer.parseInt(String s) | ||
parse4 = Integer::new; // method reference to constructor of Integer | ||
|
||
// All of the following get the length of String | ||
Function<String, Integer> | ||
len1 = s -> s.length(), // lambda expression | ||
len2 = String::length, // method reference to String#length | ||
len3 = new Function<String, Integer>() { // explicit implementation of Function interface | ||
public Integer apply(String s) { | ||
return s.length(); | ||
} | ||
}; | ||
|
||
// Function objects can be invoked with .apply() | ||
print(parse1.apply("123456")); | ||
print(len3.apply("foobar42")); | ||
|
||
// Functions may be chained with andThen and compose | ||
Function<String, String> repeatString = s -> s + s; | ||
print(parse2.compose(repeatString).apply("13579")); // invoke repeatString and then parse2 | ||
|
||
Function<Integer, Integer> squaredValue = n -> n * n; | ||
print(len2.andThen(squaredValue).apply("java10")); // invoke len2 and then squaredValue | ||
} | ||
|
||
public static void main(String[] argv) { | ||
functionExamples(); | ||
} | ||
|
||
private static void print(Object o) { | ||
System.out.println(o); | ||
} | ||
} |