|
《Java8函数式编程之Function》场景:可以应用在计算逻辑中 java代码; J2 c/ H9 H1 E f2 J, L
5 |$ f/ h; Q3 H! w# S1 z
部分java源码:
6 K8 O) ] w5 c4 D- i2 r1、
9 m* u* ^/ v, O. y- B# ^package com.javazx.jdk8;# s f* Z# l8 Y5 u- \! u
9 l1 N4 i" P1 C6 I ]- Q. s! `
import java.util.function.BiFunction;* l4 b. T) \7 s$ v. V1 a5 Q; p' n7 E
import java.util.function.Function;4 q9 _/ y; m! L2 L! J- d& U: W" Z
& j% R3 l- D" I9 ~+ N
/**2 n) k. B( ~* z8 N5 `6 s/ g2 T8 t- R
* @author: itmrchen8 Q$ Q ?5 ?( C O/ _8 {
* @Description: Function中的compose和andThen两个方法的使用
( ~% }& d. m' C * 如果是两个参数一个输出则用BiFunction/ @0 G+ d( I. F# r
* @date 2019/8/6 0:16- f& R. G$ e" D
*/
: n/ z& ^7 [2 t8 J- F0 Wpublic class FunctionTest2 {
! C! ?5 i+ x) Y# s; T& J& S6 B' n# j public static void main(String[] args) {0 ?' n: ^( }- e5 T0 i
FunctionTest2 functionTest = new FunctionTest2();
1 ?* v& S. C5 b: A- w System.out.println(functionTest.compute(2, value -> value * 3, value -> value * value));
: f; }! }! u/ l/ i System.out.println(functionTest.compute2(2, value -> value - 3, value -> value * value));2 `' |! U0 _, n3 T7 p5 L+ F6 @# s7 m
/ {/ {) j3 x& j6 b' }3 V1 p% t
System.out.println(functionTest.compute3(2, 3, (value1, value2) -> value1 + value2));
# ]6 M( `# j5 t
( b# @8 m8 a6 o System.out.println(functionTest.compute4(2, 3, (value1, value2) -> value1 + value2, value -> 2 * value));
0 N$ m" @7 e" t4 `
" D- d+ S+ x. k/ U2 o/ r9 K }0 A6 ~2 A4 J$ w+ g7 |
% ^- C# I& ^: u' i% a /**, [3 W: o/ y L* a. `* o
* 先计算function2 再将结果与function1运算
: i& k+ I% r+ R; q *+ u- Z7 m' T) c* @" H# _
* @param a, O2 |8 V# d. m7 Z' \& q
* @param function1
! ?+ N1 A" X0 e * @param function27 h- V. E: u U' d/ v
* @return/ |. b) X* p1 p) o& x
*/! Y9 s: `' [8 Z6 Y
public int compute(int a, Function<Integer, Integer> function1, Function<Integer, Integer> function2) {: \% r* c. ?) a
return function1.compose(function2).apply(a);; C. x3 A/ l: D2 Z+ Z( n# t
}
1 B9 w. L2 d- z( W% u5 M
" o3 D3 a( A3 w1 {2 Z /**' a9 |* n; ~7 j# s4 T; O2 {" A
* 先计算function1 再将结果与function2运算
v+ t+ Y {/ y0 ^. |6 \7 Q *
. T9 @) i0 ?2 n# G5 Z) f * @param a6 S" D' q7 y2 e6 H4 w
* @param function1* N3 R4 Z7 g6 x/ X8 X
* @param function2' ?' \% r, q; Y5 q, y L3 @
* @return
& R7 t4 i6 q, u */
: B! \& u* h: o8 y, e. ~! g public int compute2(int a, Function<Integer, Integer> function1, Function<Integer, Integer> function2) {
7 ]0 f/ U; d, G/ r( b return function1.andThen(function2).apply(a);
+ B7 q+ }% s* W5 w }
; ~! A' C5 T) c0 L* N! a! ]+ @/ D6 D, D4 B
/**
+ o0 T$ M/ t5 P4 v$ V * 具体操作由调用者决定
+ V, W( ]' m- ]1 Q; k *4 f* A: h" X/ S4 ~' Q. q
* @param a
8 S: s R# A k+ Z$ d * @param b
$ {9 G5 }" v7 X/ y * @param function6 L* @2 W/ s- Q0 o
* @return
! B3 O( r% B/ |4 } */% E4 t2 h: e9 J
public int compute3(int a, int b, BiFunction<Integer, Integer, Integer> function) {
& [ s( q U3 W5 p; S return function.apply(a, b);1 g8 J$ @* k# @8 m* @7 E9 E3 d- j5 |# g8 d
}8 {8 R- k3 s% e& K4 e
# G, |7 _# G0 B2 ~% y /**
0 {# g# i7 q9 n2 x( Z * @param a9 v# v5 c! ^2 o( C
* @param b5 o% i& r5 `7 M2 C3 i
* @param biFunction
7 W1 g/ e7 I, [5 A5 s: l( o3 G( l * @param function
( q" h3 ^* P+ v# Y% o * @return
1 P: ]# `* i9 ^/ X9 A6 @8 x */
8 A0 h) Q; H. `4 f public int compute4(int a, int b, BiFunction<Integer, Integer, Integer> biFunction, Function<Integer, Integer> function) {1 N1 T* q" y+ P2 y
return biFunction.andThen(function).apply(a, b);, N6 E; c; \4 ~3 m
}3 a' P- x- e' j- q9 n
2 h S( a* W( |: y& ^. G0 w$ a: d$ n3 F9 Z
}- u( [; o4 A9 p9 c! H. s/ }* ^
0 P. N4 [5 {1 T: }& y" S: E
' u: \: ~# T0 _$ y
, h9 T# P" e0 y, ~4 b5 g& D8 b
" \" |$ m `! c+ i4 E+ K6 `7 l0 m/ m) i; O1 h% z' r( c1 c
github地址:https://github.com/itmrchen/java8位置在 java8\src\main\java\com\javazx\jdk8\FunctionTest.java FunctionTest2.java9 t, b5 P' A7 [! v( \& X* E3 S% s
5 I3 C! j( V2 {/ G- y# E
2 e# B( Q; x& q
6 R$ t+ [% H+ }* h3 a8 k% ^) E4 `1 S# u
) X7 |! F! z2 c! R2 K" }/ y; q& g
|
|