본문으로 건너뛰기

Lambda와 forEach

람다와 forEach

자주 쓰이고 중요하므로 따로 빼봤다.

리스트 형

public class Exercise1 {
public static void main(String[] args) throws Exception {

List<String> fruits = Arrays.asList("Orange", "Apple", "Kiwi", "Banana", "Mango");

fruits.forEach(f -> System.out.println(f));
}

}

```git
Orange
Apple
Kiwi
Banana
Mango

맵 형
오.. 이거 좋다!

public class Exercise1 {
public static void main(String[] args) throws Exception {

Map<Integer, String> student = new HashMap<Integer, String>();

student.put(1, "David");
student.put(2, "Mike");
student.put(3, "Cavin");
student.put(4, "Bob");
student.put(5, "Mariah");

student.forEach((key, value) -> System.out.println(key + " : " + value));
}

}


```git
1 : David
2 : Mike
3 : Cavin
4 : Bob
5 : Mariah