Java 18일차 2(람다 Function)

2022. 12. 5. 09:44코딩배움일지/JAVA

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.BiFunction;
import java.util.function.Function;

public class Main4 {

	public static void main(String[] args) {
		
		Function<Integer,String> fx1 = age -> age + "살"; /*함수의 매개변수로 많이 쓰인다.*/
		 
		System.out.println(fx1.apply(20));
		
		Function<Function<Integer, String>,String> fx2 = fx -> fx.apply(20) + "입니다"; /*binary Function*/
		
		System.out.println(fx2.apply(age -> age + "살")); /*람다 안에서 람다 쓰기?*/ /*return 자체여 Function 주기?*/
		
		BiFunction<Integer, String , Map<Integer,String>> createMap = (number, name) -> {
			Map<Integer,String> map = new HashMap<>();
			map.put(number, name);
			return map;
		};
		
		List<Map<Integer,String>> list = new ArrayList<>();
		list.add(createMap.apply(100,"김준일"));
		list.add(createMap.apply(200,"유열림"));
		list.add(createMap.apply(300,"이승아"));
		list.add(createMap.apply(400,"이영인"));
		list.add(createMap.apply(500,"임지현"));
		
		System.out.println(list);
	}

}

 

Function <T, R>

R apply(T t) 

 

public interface Function<T, R> {

    /**
     * Applies this function to the given argument.
     *
     * @param t the function argument
     * @return the function result
     */
    R apply(T t);

 

BiFuntion <T, U, R>

R apply(T t, U u)

 

public interface BiFunction<T, U, R> {

    /**
     * Applies this function to the given arguments.
     *
     * @param t the first function argument
     * @param u the second function argument
     * @return the function result
     */
    R apply(T t, U u);

 

 

'코딩배움일지 > JAVA' 카테고리의 다른 글

Java 18일차 4 (문자열 메소드)  (0) 2022.12.05
Java 18일차 3(람다 Predicate)  (0) 2022.12.05
Java 18일차 1(람다 Map ForEach)  (0) 2022.12.05
Java 17일차 4-2(람다)  (0) 2022.12.02
Java 17일차 4-1(람다)  (0) 2022.12.02