1+ package com .example .Item42LambdasToAnonymous ;
2+
3+ import java .util .Arrays ;
4+ import java .util .Collections ;
5+ import java .util .Comparator ;
6+ import java .util .List ;
7+ import static java .util .Comparator .comparingInt ;
8+
9+ // Sorting with function objects (Pages 193-4)
10+ public class SortFourWays {
11+
12+ public static void main (String [] args ) {
13+
14+ String [] nameList = {"ali" ,"veli" ,"furkan" ,"berna" ,"yennefer" ,"geralt" ,"vesemir" ,"ciri" };
15+ List <String > words = Arrays .asList (nameList );
16+
17+ // Anonymous class instance as a function object - obsolete! (Page 193)
18+ Collections .sort (words , new Comparator <String >() {
19+ public int compare (String s1 , String s2 ) {
20+ return Integer .compare (s1 .length (), s2 .length ());
21+ }
22+ });
23+ System .out .println (words );
24+ Collections .shuffle (words );
25+
26+ // Lambda expression as function object (replaces anonymous class) (Page 194)
27+ Collections .sort (words , (s1 , s2 ) -> Integer .compare (s1 .length (), s2 .length ()));
28+ System .out .println (words );
29+ Collections .shuffle (words );
30+
31+ // Comparator construction method (with method reference) in place of lambda
32+ // (Page 194) Bu terminoloji madde 43 te anlatılıyor burada değinmeyeceğim.
33+ Collections .sort (words , comparingInt (String ::length ));
34+ System .out .println (words );
35+ Collections .shuffle (words );
36+
37+ // Default method List.sort in conjunction with comparator construction method
38+ // (Page 194)
39+ words .sort (comparingInt (String ::length ));
40+ System .out .println (words );
41+
42+ }
43+
44+ }
0 commit comments