File tree Expand file tree Collapse file tree 2 files changed +39
-1
lines changed
leetcode_java/src/main/java/dev/Sorting Expand file tree Collapse file tree 2 files changed +39
-1
lines changed Original file line number Diff line number Diff line change @@ -69,7 +69,7 @@ public void testSortMapWithValue(){
6969 * - The lambda expression is shorthand for an anonymous inner class that implements the compare method directly.
7070 *
7171 */
72-
72+
7373
7474 /** V1 : lambda expression */
7575// Collections.sort(keyList, (x,y) -> {
Original file line number Diff line number Diff line change 11package dev .Sorting ;
22
3+ import org .junit .Test ;
4+
5+ import java .util .*;
6+
37public class ListSortTest {
8+
9+ @ Test
10+ public void testSortListWithVal_1 (){
11+
12+ List <Integer > list = new ArrayList <>();
13+ list .add (5 );
14+ list .add (99 );
15+ list .add (1 );
16+ list .add (-3 );
17+
18+ System .out .println (">>> (before) list = " + list );
19+
20+ // V1: lambda
21+ Collections .sort (list , (x ,y ) -> {
22+ int diff = y - x ;
23+ return diff ;
24+ });
25+ //
26+ // // V2: Comparator.compare
27+ // Collections.sort(list, new Comparator<Integer>(){
28+ // @Override
29+ // public int compare(Integer o1, Integer o2) {
30+ // //int diff = o1 - o2; // smaller first (increasing order)
31+ // int diff = o2 - o1; // bigger first, (decreasing order)
32+ //// if(diff == 0){
33+ //// return
34+ //// }
35+ // return diff;
36+ // }
37+ // });
38+
39+ System .out .println (">>> (after) list = " + list );
40+
41+ }
442}
You can’t perform that action at this time.
0 commit comments