티스토리 뷰

728x90

java의 컬렉션인 List에 관한 예제에서 기본값을 넣어줄 때

List<Integer> integerList = List.of(1, 2, 3, 4, 5);

List<Integer> integerList2 = Arrays.asList(1, 2, 3, 4, 5);

of() 정적 메서드는 자바 9부터 추가된 메서드이다.

 

어쨌든 List 값을 넣어주는 것은 동일한 것 같은데 무슨 차이일까 궁금해서 검색해봄.

 

Arrays.asList returns a mutable list while the list returned by List.of is immutable:

 

asList는 불변객체가 아니고 of는 불변객체로 만들어준다.

 

크게 뭔가 와닿는건 없는데 stackoverflow를 첨부 나중에 더 깨닫게되면 다시 씀.

 

stackoverflow.com/questions/46579074/what-is-the-difference-between-list-of-and-arrays-aslist

 

What is the difference between List.of and Arrays.asList?

Java 9 introduced new factory methods for lists, List.of: List strings = List.of("first", "second"); What's the difference between the previous and the new option? That is, what's the

stackoverflow.com

 

728x90
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
«   2024/05   »
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 28 29 30 31
글 보관함