package tryAny.effectiveJava;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
public class AnonymouseVsLambda {
public static void main(String[] args) {
List<String> words1 = Arrays.asList("apple", "pen", "pineapple");
//Write in an anonymous class.
Collections.sort(words1, new Comparator<String>() {
public int compare(String s1, String s2) {
return Integer.compare(s1.length(), s2.length());
}
});
System.out.println(words1);
//If the type is Raw, a compile error will occur.
List<String> words2 = Arrays.asList("banana", "grape", "melon");
//Write in a lambda expression.
Collections.sort(words2, (s1, s2) -> Integer.compare(s1.length(), s2.length()));
System.out.println(words2);
}
}
Recommended Posts