Consumer

 

리턴값이 없는 accept() 메소드를 가지고 있다. (매개변수 소비자 역할)
Consumer<T>#accept(T t) : void : 객체 T를 받아 소비한다.
전달된 문자열 -> 문자열을 포함한 걸 출력하겠다. 아래 람다식으로 구현
accept("hello world"); +(str + "() 입력됨") 포함된 출력이 일어난다.
Consumer<String> consumer = str -> System.out.println(str + "이(가) 입력됨");
consumer.accept("hello world");

> hello world이(가) 입력됨

BiConsumer

BiConsumer<T, U>#accept(T t, U u) : void : 객체 T, U를 받아 소비
로컬 타임객체 만들어서 전달하면,"hello world"가 언제 입력되었는지 알 수 있
2가지 타입을 받을 수 있는 것이 BiConsumer이다
BiConsumer<String, LocalTime> biConsumer = (str1, time) -> System.out.println(str1 + "이(가) " + time + "에 입력됨");
biConsumer.accept("hello world", LocalTime.now());

> hello world이(가) 09:59:19.503886900에 입력됨

 

Supplier

 

매개변수가 없고 리턴 값이 있는 getXXX() 메소드를 가지고 있다.
Supplier<T>#get() : T : 객체 T를 리턴한다, 다양한 값 리턴 가
Supplier<LocalDateTime> supplier = () -> LocalDateTime.now();
System.out.println(supplier.get());

>2023-04-06T13:34:11.683900100

IntSupplier#getAsInt() : int : int값을 리턴한다.
DoubleSupplier#getAsDouble() : double : double값을 리턴한다
IntSupplier intSupplier = () -> (int) (Math.random() * 6) + 1;
System.out.println("주사위를 던져서 나온 수 : " + intSupplier.getAsInt());

> 주사위를 던져서 나온 수 : 1

DoubleSupplier doubleSupplier = () -> Math.random();
System.out.println("Math.random()의 리턴값 : " + doubleSupplier.getAsDouble());

> Math.random()의 리턴값 : 0.3042715201390368

 

Function

 

매개변수와 리턴값이 있는 applyXXX() 메소드를 가지고 있다. (매개변수를 리턴값으로 매핑하는 역할)
Function : 매개변수와 리턴값이 있는 applyXXX() 메소드를 가지고 있다. (매개변수를 리턴값으로 매핑하는 역할)
Function<T, R>#apply(T t) : R : 객체 T를 R로 매핑한다.
Function은 매개변수의 타입, 반환 값의 타입을 이미
String값을 받아서 Integer로 반환한다 
Function<String, Integer> function = (str) -> Integer.parseInt(str);
String strValue = "12345";
System.out.println(function.apply(strValue) + " : " + function.apply(strValue).getClass().getName());

> 12345 : java.lang.Integer

 

Operator

 

Function과 똑같이 applyXXX() 메소드를 가지고 있다. 차이점은 매개변수로 연산을 한 후 통일타입으로 리턴
매개변수와 리턴값이 있는 applyXXX() 메소드를 가지고 있다.
BinaryOperator<String> binaryOperator = (str1, str2) -> str1 + str2;
System.out.println(binaryOperator.apply("hello", "world"));

> helloworld

 

Predicate

 

매개변수와 boolean 값을 반환하는 testXXX()를 가지고 있다. (매개변수를 활용하여 boolean 반환)
 이 메소드들은 매개변수 값을 이용하여 true 혹은 false값을 리턴하는 역할을 한다.
 Predicate<T>#test(T t) : boolean : T를 조사하여 boolean을 리턴한다. */
Predicate<Object> predicate = value -> value instanceof String;
System.out.println("문자열인지 확인 : " + predicate.test("123"));
System.out.println("문자열인지 확인 : " + predicate.test(123));

>

메소드 참조(method references)

 

함수형 인터페이스를 람다식이 아닌 일반 메소드를 참조시켜 선언하는 방식이다.
일반 메소드를 참조하기 위해서는 함수형 인터페이스의 매개변수 타입/갯수/반환형과 메소드의 타입/갯수/반환형이 같아야 한다.

메소드 참조 표현식
클래스 이름::메소드 이름
참조변수이름::메소드 이름

 

이 아래 코드의 설명은 String type을 2개를 전달해서 Boolean타입으로 반환하는 걸 전달하겠다 라는 의미이다.

	BiFunction<String, String, Boolean> biFunction = String::equals;
	System.out.println(biFunction.apply("java", "java"));
	
	List<String> subjects = new ArrayList<>();
	subjects.add("java");
	subjects.add("oracle");
	subjects.add("jdbc");
	subjects.add("html");

> true

 

 

아래는 consumer를 참조할 수 있는 것이 아니라서 consumer의 accept이랑 맞는 메소드이다.

 

	forEach(subjects, System.out::println);
}
	
	private static void forEach(List<? extends Object> list, Consumer<Object> consumer) {
		for(Object obj : list) {
			consumer.accept(obj);
		}
	}

>

java

oracle

jdbc

html

 

생성자도 메소드를 참조할 수 있다.

		Function<String, Account> function = Account::new;
		
		Account account1 = function.apply("홍길동");
		Account account2 = function.apply("유관순");
		
		System.out.println(account1);
		System.out.println(account2);
	}

>

Account [ownerName=홍길동, balance=0]

Account [ownerName=유관순, balance=0]

 

SMALL

'java' 카테고리의 다른 글

Stream을 사용해 보자  (0) 2023.04.06
람다(lambda)식 사용해보기  (0) 2023.04.05
ENUM 열거형이란?  (0) 2023.04.05
java Exception13  (1) 2023.01.09
java Collection12-Set,Linked,Map  (0) 2023.01.09

+ Recent posts