import java.util.*;
class Solution {
public static void main(String[] args) {
sort1();
sort2();
}
public static void sort1 () {
List<Integer> list1 = Arrays.asList(3,2,8,4);
System.out.println("Avant le tri: "+list1);
Comparator<Integer> comparator = new Comparator<Integer>() {
public int compare(Integer int1, Integer int2) {
return (int1.compareTo(int2));
}
};
Collections.sort(list1, comparator);
System.out.println("comparator(Instancier),Après le tri: "+list1);
}
public static void sort2(){
List<Integer> list1 = Arrays.asList(3,2,8,4);
System.out.println("Avant le tri: "+list1);
Collections.sort(list1, (a, b) -> Integer.compare(a, b));
System.out.println("Comparateur Lambda,Après le tri: "+list1);
}
}
Avant le tri: [3, 2, 8, 4]
comparator(Instancier),Après le tri: [2, 3, 4, 8]
Avant le tri: [3, 2, 8, 4]
Comparateur Lambda,Après le tri: [2, 3, 4, 8]
Recommended Posts