아래의 글은 라울-게이브리얼 우르마, 마리오 푸스코, 앨런 마이크로프트 저/우정은 역, 『모던 자바 인 액션』,한빛미디어(2019), CH01의 내용을 기반으로 작성하였습니다.

람다

표현식

(파라메터리스트) ->(화살표) { 바디) }

(String s) -> s.length()

(Apple a) -> a.getWeight() > 150

(int x, int y) -> { System.out.println(“sum=”+(x+y))

() -> 42

(String s1, String s2) -> s1.equals(s2)

 

함수형 인터페이스

추상메소드가 하나면 함수형 인터페이스

@FuntionalInterface

하나인거 보장

객체 생성후 override 없이 람다사용가능

 

함수 descriptor

함수형 인터페이스의 추상메서드의 시그너쳐

람다 표현식을 사용하기 위한 시그너처

표준화 -> 인터페이스

함수형 인터페이스의 시그너쳐의 표준화

타입은 모두 Object만 가능 : primitive type boxing

Predicate<T> – input T, return Boolean : boolean test(T t)

Consumer<T> – input T, return void : void accept(T t)

Function<T, R> - input T, return R : R apply(T t)

 

제공되는 함수형 인터페이스

함수형 인터페이스 함수 디시크립터 예제
Predicate<T> T -> boolean (List<String> list) -> list.isEmpty()
Consumer<T> T -> void (Apple a) -> System.out.println(a.getWeight)
Function<T> T -> R () -> new Apple(10)
Supplier<T> () -> T  
UnaryOperator<T> T -> T  
BiOperator<T> (T, T) -> T  
BiPredicate<L, R> (L, R) -> boolean  
BiConsumer<T, U> (T, U) -> void  
BiFunction<T, U, R> (T, U) -> R (Apple a1, Apple a2) ->
a.getWeight().compareTo(a2.getWeight())

 

메서드레퍼런스, 생성자 레퍼런스

(String s) -> Integer.parseInt(s)

Integer::parseInt

New Apple

Apple::new

 

형식추론

List<Apple> filteredAppleList = filter(appleList, (Apple a) -> a.getWeight() > 10);

List<Apple> filteredAppleList 2 = filter(appleList, a -> a.getWeight() > 10);

스트림, Stream

입출력 : stream

비디오 : 스트리밍

Stream processing : SIMD , 그래픽 병렬처리

SIMD : Single Instruction Multiple Data

리눅스 pipe

Java API : java.util.stream, java.util.function

 

Java 8 : Stream

 

스트림 특징

Collection 데이터 처리의 추상화

스트림 소스 : collection, array, io

Stream pipeline (스트림 연산은 스트림 자신을 반환)

Lazy 연산

Stream요소추가 및 요소 제거 불가

Steam은 한번만 탐색가능

 

참고사이트 :

dzone.com/articles/java-8-streams-api-laziness

 

Java 8 Streams API: Laziness and Performance Optimization - DZone Java

In this post, we will move ahead with the Java 8 Streams API and have a look at the most important property of Java 8 Streams — laziness.

dzone.com

 

+ Recent posts