88
99import static java .util .Comparator .comparingInt ;
1010
11+ // Sorting with function objects (Pages 193-4)
1112public class SortFourWays {
1213 public static void main (String [] args ) {
1314 List <String > words = Arrays .asList (args );
1415
15- // Anonymous class instance as a function object - obsolete!
16+ // Anonymous class instance as a function object - obsolete! (Page 193)
1617 Collections .sort (words , new Comparator <String >() {
1718 public int compare (String s1 , String s2 ) {
1819 return Integer .compare (s1 .length (), s2 .length ());
@@ -21,16 +22,18 @@ public int compare(String s1, String s2) {
2122 System .out .println (words );
2223 Collections .shuffle (words );
2324
24- // Lambda expression as function object (replaces anonymous class)
25+ // Lambda expression as function object (replaces anonymous class) (Page 194)
2526 Collections .sort (words ,
2627 (s1 , s2 ) -> Integer .compare (s1 .length (), s2 .length ()));
2728 System .out .println (words );
2829 Collections .shuffle (words );
2930
31+ // Comparator construction method (with method reference) in place of lambda (Page 194)
3032 Collections .sort (words , comparingInt (String ::length ));
3133 System .out .println (words );
3234 Collections .shuffle (words );
3335
36+ // Default method List.sort in conjunction with comparator construction method (Page 194)
3437 words .sort (comparingInt (String ::length ));
3538 System .out .println (words );
3639 }
0 commit comments