-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathImmutableMain2.java
More file actions
27 lines (25 loc) · 1.2 KB
/
Copy pathImmutableMain2.java
File metadata and controls
27 lines (25 loc) · 1.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
package functional;
import java.util.List;
/*
불변성(Immutable State) 지향
데이터는 생성된 후 가능한 한 변경하지 않고, 변경이 필요한 경우 새로운 값을 생성해서 사용한다.
가변 데이터 구조에서 발생할 수 있는 오류를 줄이고, 프로그램 예측 가능성을 높여준다.
불변성은 데이터를 변경하지 않기 때문에 부수 효과와도 관련이 있다.
*/
public class ImmutableMain2 {
public static void main(String[] args) {
MutablePerson m1 = new MutablePerson("Kim", 10);
MutablePerson m2 = new MutablePerson("Lee", 20);
List<MutablePerson> originList = List.of(m1,m2);
System.out.println("orginList = " + originList);
List<MutablePerson> resultList = originList.stream()
.map(p -> {
p.setAge(p.getAge() + 1); // 참조 객체 값이 바뀜
// 가변 생성은 예상치 못한 결과를 초래한다.
return p;
}).toList();
System.out.println("=== 실행 후 ===");
System.out.println("originList = " + originList);
System.out.println("resultList = " + resultList);
}
}