Java의 ArrayList를 변경불가능(Immutable)로 생성하는 방법에 대해 알아봅니다.
ArrayList를 변경불가능하게 만들기 위해서는 JDK에서 지원하는 방법, Guava 라이브러리를 이용하는 방법 그리고 Apache Commons Collections 라이브러리를 이용하는 방법 세가지가 있습니다.
JDK에서 지원
1) Collections 클래스를 이용하는 방법
Collections.unmodifiableList(list);
2) Java9
Java9에서는 List.of 인 static factory method를 이용해서 리스트를 생성하면 됩니다.
ArrayList<String> list = List.of("a","b","c");
Guava 라이브러리
ImmutableList.copyOf(list);
Apache Commons Collections 라이브러리
ListUtils.unmodifiableList(list);
'개발&프로그래밍' 카테고리의 다른 글
[IntelliJ] SonarLint 플러그인 (0) | 2022.03.18 |
---|---|
[JAVA] Java의 String Empty 와 Blank 체크하기 (0) | 2022.03.18 |
[SpringSecurity] SpringSecurity에서 user detail 정보를 획득하기 (0) | 2022.03.17 |
[JUnit] JUnit5 기본 설정 및 어노테이션 (0) | 2022.03.16 |
[JAVA] Java8 Stream API - findFirst() 와 findAny() (0) | 2022.03.15 |
댓글