import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.BiConsumer;
public class BiConsumerTest {
public static void main(String... strings) {
// 기본: BiConsumer<T, U> : 두 입력 → 출력 없음 (side effect)
BiConsumer<String, Integer> printer = (name, age) -> System.out.println(name + "의 나이는 " + age + "세");
printer.accept("Alice", 30); // Alice의 나이는 30세
// Map 활용
Map<String, Integer> scores = new HashMap<>();
scores.put("Alice", 90);
scores.put("Bob", 85);
scores.forEach((name, score) -> System.out.println(name + "'s score: " + score));
// Bob's score: 85
// Alice's score: 90
// 컬렉션 요소 조합 처리
List<String> names = Arrays.asList("Alice", "Bob", "Charlie");
List<Integer> ages = Arrays.asList(25, 30, 35);
BiConsumer<String, Integer> combine = (name, age) -> System.out.println(name + " - " + age);
for (int i = 0; i < names.size(); i++) {
combine.accept(names.get(i), ages.get(i));
// Alice - 25
// Bob - 30
// Charlie - 35
}
// andThen 체이닝
BiConsumer<String, Integer> printName = (name, age) -> System.out.print("Name: " + name);
BiConsumer<String, Integer> printAge = (name, age) -> System.out.println(", Age: " + age);
BiConsumer<String, Integer> combined = printName.andThen(printAge);
combined.accept("Alice", 25); // Name: Alice, Age: 25
// 로깅, DB 업데이트 동시 처리
BiConsumer<String, Integer> logAndSave = (user, score) -> {
System.out.println("Saving " + user + " with score " + score);
// db.save(user, score); // DB 저장 로직
};
logAndSave.accept("Alice", 95); // Saving Alice with score 95
}
}