Notice
Recent Posts
Recent Comments
Link
«   2024/09   »
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
Tags
more
Archives
Today
Total
관리 메뉴

DevYGwan

ArrayList<>(), List.of(), Arrays.asList() 차이점 본문

Study/JAVA

ArrayList<>(), List.of(), Arrays.asList() 차이점

YGwan 2022. 11. 16. 14:47

 

 

ArrayList로 초기화 하는 가장 기본적인 방법은 크게 3가지가 존재합니다.

 

  1. add() 메소드 사용
  2. asList() 메소드 사용
  3. List.of() 메소드 사용

 

이들은 ArrayList를 초기화할때 주로 사용하는 방법으로 사용 방법은,

 

1. add() 메소드 사용

public class Main {
    public static void main(String[] args) {
        List<Integer> exampleList = new ArrayList<>();
        exampleList.add(1);
        exampleList.add(2);
        exampleList.add(3);
        exampleList.add(4);
        exampleList.add(5);
        
        System.out.println(exampleList);
    }
}

 

2. add() 메소드로 선언과 동시에 할당

public class Main {
    public static void main(String[] args) {
        List<Integer> exampleList = new ArrayList<>() {
            {
                add(1);
                add(2);
                add(3);
                add(4);
                add(5);
            }
        };

        System.out.println(exampleList);
    }
}

 

3. Arrays.asList() 사용

public class Main {
    public static void main(String[] args) {
        List<Integer> exampleList = Arrays.asList(1,2,3,4,5);
        
        System.out.println(exampleList);
    }
}

 

4. List.of 사용

public class Main {
    public static void main(String[] args) {
        List<Integer> exampleList = List.of(1,2,3,4,5);
        
        System.out.println(exampleList);
    }
}

이런 식으로 간단하게 사용할 수 있습니다.

 


이때 이들 사이의 중요한 차이점은 객체 타입(인스턴스 타입)이 다르다는 점입니다.

// ArrayList<>()에 관한 내용 

public ArrayList(Collection<? extends E> c) {
    Object[] a = c.toArray();
    if ((size = a.length) != 0) {
        if (c.getClass() == ArrayList.class) {
            elementData = a;
        } else {
            elementData = Arrays.copyOf(a, size, Object[].class);
        }
    } else {
        // replace with empty array.
        elementData = EMPTY_ELEMENTDATA;
    }
}


// List.of()에 관한 내용

static <E> List<E> of(E e1, E e2, E e3, E e4, E e5) {
    return new ImmutableCollections.ListN<>(e1, e2, e3, e4, e5);
}


// Arrays.asList()에 관한 내용

public static <T> List<T> asList(T... a) {
    return new ArrayList<>(a);
}



그렇다면 이 차이가 "어떤 식으로 작용할 수 있나?" 가 중요한 문제가 될 것 같습니다.

 

답은 바로

            Immutable

에 있습니다.

 

1. new ArrayList<>() 사용

흔히 new ArrayList<>() 을 사용하는 경우 기본적으로 값 추가, 삭제, 수정 등이 가능합니다.

  • 값 추가 - add() 메소드 사용
  • 값 수정 - set() 메소드 사용
  • 값 제거 - remove() 메소드 사용

 

2. List.of() 사용

List.of의 경우 값을 추가를 하거나 수정을 하려고 하면 에러를 발생시킵니다. 왜냐하면 해당 객체는 Immutable 객체이기 때문입니다.

 

3. Arrays.asList()

Arrays.asList()가 반환하는 ArrayList 타입은 java.util.ArrayList가 가 아니라 Arrays 내부 클래스입니다. add와 remove는 구현되어 있지 않아서, 크기는 변경할 수 없습니다. 하지만 set은 가능합니다.

 

 

 

List.of(), Arrays.asList() 정리

  • Arrays.asList()는 null값 허용하지만 List.of()는 null값을 허용하지 않습니다.
  • Arrays.asList()는 변경이 가능하고, List.of()는 변경이 불가능합니다.
  • Arrays.asList(), List.of() 모두 크기는 변경할 수 없습니다.

 

 

'Study > JAVA' 카테고리의 다른 글

"좋은 코드"의 작성 기술  (2) 2024.01.22
JAVA 특징 정리  (0) 2023.01.04