File tree Expand file tree Collapse file tree 1 file changed +35
-0
lines changed
src/main/java/com/arpit/java8/function Expand file tree Collapse file tree 1 file changed +35
-0
lines changed Original file line number Diff line number Diff line change 1+ package com .arpit .java8 .function ;
2+
3+ import java .util .function .Function ;
4+
5+ public class FunctionComposition {
6+
7+ private Function <Integer , Integer > times2 = e -> e * 2 ;
8+
9+ private Function <Integer , Integer > squared = e -> e * e ;
10+
11+ /**
12+ * Reference :
13+ * http://www.deadcoderising.com/2015-09-07-java-8-functional-composition-
14+ * using-compose-and-andthen/
15+ *
16+ * @param args
17+ */
18+ public static void main (String [] args ) {
19+ FunctionComposition obj = new FunctionComposition ();
20+ obj .run ();
21+
22+ }
23+
24+ /**
25+ * While the compose function executes the caller last and the parameter
26+ * first, the andThen executes the caller first and the parameter last.
27+ */
28+ private void run () {
29+ int result = times2 .compose (squared ).apply (4 ); // Returns 32
30+ System .out .println (result );
31+ int result1 = times2 .andThen (squared ).apply (4 ); // Returns 64
32+ System .out .println (result1 );
33+ }
34+
35+ }
You can’t perform that action at this time.
0 commit comments