1111import java .util .function .BiPredicate ;
1212import java .util .function .Consumer ;
1313import java .util .function .Function ;
14+ import java .util .function .IntConsumer ;
1415
1516import test .baeldung .doublecolon .Computer ;
1617import test .baeldung .doublecolon .MacbookPro ;
1718
1819public class Test_Java8 extends Test_ implements PropertyChangeListener {
1920
21+ public Test_Java8 () {
22+ System .out .println ("null constructor" );
23+ }
24+
25+ public Test_Java8 (String s ) {
26+ System .out .println ("new Test_Java8(" + s + ")" );
27+ }
28+
2029 public static void test () {
2130
2231 }
2332
2433 enum E {
2534 E1 , E2 ;
2635 }
27- int test1 = 1 ;
36+ int test1 = 1 ;
2837
2938 Object test2 (Object o ) {
3039 System .out .println ("test1 is " + test1 + " " + o );
3140 return o ;
41+ };
42+
43+ static int test1s = 2 ;
44+
45+ static Integer test2is (Integer i ) {
46+ System .out .println ("test1s is " + test1s + " " + i );
47+ return i ;
48+ };
49+
50+ Integer test2i (Integer i ) {
51+ System .out .println ("test1 is " + test1 + " " + i );
52+ return i ;
3253 };
3354
3455 public static boolean isMoreThanFifty (int n1 , int n2 ) {
@@ -45,9 +66,6 @@ public static List<Integer> findNumbers(List<Integer> l, BiPredicate<Integer, In
4566 return newList ;
4667 }
4768
48- public Test_Java8 () {System .out .println ("null constructor" );}
49- public Test_Java8 (Object o ) {System .out .println ("o is " + o );}
50-
5169 @ FunctionalInterface
5270 public interface ITest {
5371 Test_Java8 c (Object o );
@@ -56,13 +74,32 @@ public interface ITest {
5674 public static void main (String [] args ) {
5775
5876 Comparator comp = Comparator .naturalOrder ();
77+
78+ new Test_Java8 ().propertyChange (null ); // checking for nonqualified
79+
80+ ITest t = new ITest () {
81+ @ Override
82+ public Test_Java8 c (Object o ) {
83+ return new Test_Java8 (o + "??" );
84+ }
85+ };
86+ Test_Java8 test = t .c ("new" );
87+
88+ System .out .println ("what is test? " + test );
89+
5990
6091
6192 System .out .println ("E.E1 is " + E .E1 );
6293
94+
95+
6396 final String function = "" + new Date ();
6497
65- int [][][] x = new int [5 ][][];
98+
99+ // Function
100+
101+ int [][][] a ;
102+
66103 Function <Integer , int [][][]> ifi = new Function <Integer , int [][][]>() {
67104
68105 @ Override
@@ -71,49 +108,72 @@ public int[][][] apply(Integer t) {
71108 return new int [t .intValue ()][][];
72109 }
73110 };
74- int [][][] a = ifi .apply (5 );
111+ a = ifi .apply (5 );
112+ System .out .println ("a length is " + a .length );
75113
76114 a = (new Function <Integer , int [][][]>() {
77115 @ Override
78116 public int [][][] apply (Integer t ) {
79117 return new int [t .intValue ()][][];
80118 }
81- }).apply (5 );
82-
119+ }).apply (6 );
120+ System .out .println ("a.length is " + a .length );
121+
122+ // // Lambda_C
123+
83124 Function <Integer , int [][][]> iaCreator = int [][][]::new ;
84- a = iaCreator .apply (5 );
125+ a = iaCreator .apply (7 );
85126 System .out .println ("a.length is " + a .length );
86- Test_Java8 [] atest = new Test_Java8 [5 ];
87- // following format only works for
88- Function <Object ,Test_Java8 > iaCreator2 = Test_Java8 ::new ;
127+
128+ Function <String ,Test_Java8 > iaCreator2 = Test_Java8 ::new ;
129+ iaCreator2 .apply ("testNew" );
130+
89131
90- ITest t ;
91- t = Test_Java8 ::new ;
92- t = new ITest () {
93- @ Override
94- public Test_Java8 c (Object o ) {
95- return new Test_Java8 (o + "??" );
96- }
97- };
98- Test_Java8 test = t .c ("new" );
99-
100- System .out .println ("what is test? " + test );
101132 Consumer <String > c = s -> System .out .println (s );
133+ c .accept ("testingPointer" );
134+
102135 c = System .out ::println ;
103- c .accept ("testing " );
136+ c .accept ("testingDoubleColon " );
104137
105-
106-
107- Function <Consumer <String >, ?> f = new Test_Java8 (null )::test2 ;
108- // Q: where is the object in this expression? test.test2.apply(test, [args])
109- f .apply (c );
138+ Function <Integer , Integer > f1 = new Test_Java8 ("for f1" )::test2i ;
139+ f1 .apply (new Integer (3 ));
140+
141+ Function <Integer , Integer > f2 = Test_Java8 ::test2is ;
142+ f2 .apply (new Integer (3 ));
143+
144+ Function <Consumer <String >, ?> f = new Test_Java8 ("for f" )::test2 ;
145+ f .apply (c );
110146
111147 Test_Java8 t8 = new Test_Java8 ("new8" );
112148 f = t8 ::test2 ;
113149 f .apply (c );
150+ f .apply (System .out ::println );
151+
152+ // lambda_M
114153
115- t8 .propertyChange (null ); // checking for nonqualified
116-
154+ Computer c1 = new Computer (2015 , "white" , 100 );
155+ Computer c2 = new MacbookPro (2009 , "black" , 100 );
156+ List <Computer > inventory = Arrays .asList (c1 , c2 );
157+
158+ inventory .forEach (System .out ::println ); // works because System.out.println(Object) exists
159+
160+ // note that YOU CANNOT HAVE BOTH static Computer.turnOnPc(Object) and computer.turnOnPc()
161+ // so it should be sufficient to call one or the other:
162+ // Computer.turnOnPc$Computer.apply(x)
163+ // or
164+ // Computer.prototype.turnOnPc$.apply()
165+
166+ inventory .forEach (Computer ::turnOnPc ); // works because c.turnOnPc() exists
167+ inventory .forEach (Computer ::turnOffPcStatic ); // works because Computer.turnOffPcStatic(Computer) exists
168+
169+
170+
171+ // x -> y() lambda_E
172+
173+ Function <Integer , String > f3 = i -> Integer .toHexString (i );
174+ System .out .println ("testing->" + f3 .apply (new Integer (300 )));
175+
176+
117177 List <Integer > list = new ArrayList <Integer >();
118178 list .add (Integer .valueOf (30 ));
119179 list .add (Integer .valueOf (50 ));
@@ -122,15 +182,6 @@ public Test_Java8 c(Object o) {
122182
123183 System .out .println (findNumbers (list , bp ).size ());
124184
125- long j = 1 ;
126- int i = 3 ;
127- j &= ~(1L << i );
128- j = 1L << i ;
129- j = 1L << 3 ;
130- j &= 1L << i ;
131- j = ~i ;
132- j = ~(1L << i );
133-
134185 System .out .println ("=== RunnableTest ===" );
135186
136187 // Anonymous Runnable
@@ -150,12 +201,6 @@ public void run() {
150201 r2 .run ();
151202
152203
153- Computer c1 = new Computer (2015 , "white" , 100 );
154- Computer c2 = new MacbookPro (2009 , "black" , 100 );
155- List <Computer > inventory = Arrays .asList (c1 , c2 );
156- inventory .forEach (System .out ::println );
157- inventory .forEach (Computer ::turnOnPc );
158- inventory .forEach (Computer ::turnOffPcStatic );
159204 System .out .println ("Test_Java8 OK" );
160205 }
161206
0 commit comments