-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjavaBiFunction.java
More file actions
41 lines (27 loc) · 1.24 KB
/
javaBiFunction.java
File metadata and controls
41 lines (27 loc) · 1.24 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 com.home;
import java.util.*;
import java.util.function.BiFunction;
import java.util.function.Function;
import java.util.stream.Collectors;
public class javaBiFunction {
public static void main(String[] args) {
// takes two Integers and return an Integer
BiFunction<Integer, Integer, Integer> func = (x1, x2) -> x1 + x2;
Integer result = func.apply(2, 3);
System.out.println(result); // 5
// take two Integers and return an Double
BiFunction<Integer, Integer, Double> func2 = (x1, x2) -> Math.pow(x1, x2);
Double result2 = func2.apply(2, 4);
System.out.println(result2); // 16.0
// take two Integers and return a List<Integer>
BiFunction<Integer, Integer, List<Integer>> func3 = (x1, x2) -> Arrays.asList(x1 + x2);
List<Integer> result3 = func3.apply(2, 3);
System.out.println(result3);
// Math.pow(a1, a2) returns Double
BiFunction<Integer, Integer, Double> func1 = (a1, a2) -> Math.pow(a1, a2);
// takes Double, returns String
Function<Double, String> func2s = (input) -> "Result : " + String.valueOf(input);
String results = func1.andThen(func2s).apply(2, 4);
System.out.println(results);
}
}