File tree Expand file tree Collapse file tree 1 file changed +13
-0
lines changed
Chapter09/P199_StreamComparators/src/main/java/modern/challenge Expand file tree Collapse file tree 1 file changed +13
-0
lines changed Original file line number Diff line number Diff line change 22
33import java .util .ArrayList ;
44import java .util .Comparator ;
5+ import static java .util .Comparator .comparing ;
6+ import static java .util .Comparator .nullsLast ;
57import java .util .List ;
68
79public class Main {
@@ -157,5 +159,16 @@ public static void main(String[] args) {
157159 (s1 , s2 ) -> Character .compare (s1 .charAt (s1 .length () - 1 ),
158160 s2 .charAt (s2 .length () - 1 )))))
159161 .forEach (System .out ::println );
162+
163+ System .out .println ();
164+
165+ // same result as the previous example but more readable
166+ Comparator <String > byCharAt = nullsLast (
167+ (s1 , s2 ) -> Character .compare (s1 .charAt (s1 .length () - 1 ),
168+ s2 .charAt (s2 .length () - 1 )));
169+ Comparator <Car > byFuelAndCharAt = comparing (Car ::getFuel , byCharAt );
170+ cars .stream ()
171+ .sorted (byFuelAndCharAt )
172+ .forEach (System .out ::println );
160173 }
161174}
You can’t perform that action at this time.
0 commit comments