Item 42. Lambda
class Main {
public static void main(String[] args) {
Collections.sort(words, new Comparator<String>() {
public int compare(String o1, String o2) {
return Integer.compare(o1.length(), o2.length());
}
});
}
}class Main {
public static void main(String[] args) {
/*
1. λλ€ ννμ
- λ§€κ° λ³μμ λ°ν κ°μ νμ
μλ΅(μ»΄νμΌλ¬κ° λ¬Έλ§₯μ μ΄ν΄ νμ
μ μΆλ‘ )
- return λ¬Έ μλ΅(λ¨μΌ ννμμ΄λ―λ‘ μλμΌλ‘ λ°ν)
*/
Collections.sort(words, (o1, o2) -> Integer.compare(o1.length(), o2.length()));
/*
2. λΉκ΅μ μμ± λ©μλ μ¬μ©
*/
Collections.sort(words, comparingInt(String::length));
/*
3. List.sort μ¬μ©
*/
words.sort(comparingInt(String::length));
}
}λλ€μ νκ³
Last updated