public static List<Student> upper(int score) {
List<Student> list = new ArrayList<>();
// 반복문 돌기
for (int i = 0; i < studentList.size(); i++) {
if (studentList.get(i).getScore() > score) {
liset.add(studentList.get(i));
}
}
// 함수형 프로그래밍 (알고리즘 짤 필요 없다)
return studentList.stream()
.filter(s -> s.getScore() > socre)
.sorted(Comparator.comparint(Student::getScore)
.collect(Collectors.toList());
}
public static void main(String[] args) {
Func func = i -> i + 10;
System.out.println(func.method(1)); // 11
System.out.println(func.method(1)); // 11 // 12 이면 안됨
System.out.println(func.method(1)); // 11
}
// 메인 스레드에 할당된 변수
// 초기화 후 수정 안하면 컴파일러가 final 처리 (effectively final)
int num = 10;
System.out.println("num = " + num);
// 람다에서 외부 변수 사용
Runnable runnable1 = () -> System.out.println("runnable1 = " + num);
Runnable runnable2 = () -> System.out.println("runnable2 = " + num);
runnable1.run();
runnable2.run();
// 외부 변수가 수정되면 람다 사용 불가 (컴파일 에러)
// num++;
public class Main {
public static void main(String[] args) {
Func func = s -> System.out.println(s); // 함수를 변수로 저장
func.method("this is functional interface");
}
}
public class Main {
public static void main(String[] args) {
List<Integer> values = Arrays.asList(7, 5, 123, 5, 42, 95, 68, 30, 42);
List<Integer> result = values.stream()
.filter(number -> number < 50) // 함수의 인자로 함수를 전달
.distinct()
.sorted(Integer::compare)
.collect(Collectors.toList());
System.out.println("result = " + result);
}
}
public class GoodCode {
public int add() {
return 3 + 5;
}
public static void main(String[] args) {
new GoodCode().add(); // 8
new GoodCode().add(); // 8
// ....
new GoodCode().add(); // 8
}
}
public class BadCode {
public void print() {
System.out.println("hello world.");
}
public static void main(String[] args) {
new BadCode().print();
}
}