학습 내용/책 내용 및 회고
모던 자바 인 액션 - 2022-3-30
ohksj77
2022. 3. 30. 23:44
Apple의 weight를 비교하는 예제에서 :: 는 해당 메소드를 활용하라는 메서드 참조이다. :: 왼쪽에 클래스를, 오른쪽에 활용할 메서드명을 적으면 된다.
public class Main {
public static void main(String[] args) {
List<Apple> inventory = Arrays.asList(
new Apple(80, "green"),
new Apple(155, "green"),
new Apple(120, "red")
);
List<Apple> heavyApples = inventory.parallelStream().filter((Apple a) -> {
return a.getWeight() > 150;
}).collect(toList());
}
}
Apple 클래스에는 color라는 String 필드가 추가되었다.
병렬처리stream을 통한 Apple의 weight가 150초과인 경우만 필터링하는 코드이다.
List<Apple> heavyApples = inventory.parallelStream().filter((Apple a) ->
a.getWeight() > 150).collect(toList());
위 람다식이 있는 코드를 이렇게도 쓸 수 있다.