File tree 1 file changed +13
-0
lines changed
Chapter09/P199_StreamComparators/src/main/java/modern/challenge
1 file changed +13
-0
lines changed Original file line number Diff line number Diff line change 2
2
3
3
import java .util .ArrayList ;
4
4
import java .util .Comparator ;
5
+ import static java .util .Comparator .comparing ;
6
+ import static java .util .Comparator .nullsLast ;
5
7
import java .util .List ;
6
8
7
9
public class Main {
@@ -157,5 +159,16 @@ public static void main(String[] args) {
157
159
(s1 , s2 ) -> Character .compare (s1 .charAt (s1 .length () - 1 ),
158
160
s2 .charAt (s2 .length () - 1 )))))
159
161
.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 );
160
173
}
161
174
}
You can’t perform that action at this time.
0 commit comments