Java & Kotlin
List의 of() 와 ArrayList.asList() 차이
터프남
2021. 2. 22. 18:02
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
반응형