๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
Java·๏ปฟServlet·๏ปฟJSP

[JAVA] Arrays.asList()์™€ List.of()์˜ ์ฐจ์ด

by Leica 2023. 2. 10.
๋ฐ˜์‘ํ˜•

 

 

JAVA๋Š” List ๊ฐ์ฒด๋ฅผ ๋งŒ๋“ค๊ฑฐ๋‚˜ array๋ฅผ List๋กœ ๋ณ€ํ™˜(array -> list)ํ•˜๊ธฐ ์œ„ํ•ด ํฌ๊ฒŒ ๋‘ ๊ฐ€์ง€ ๋ฉ”์†Œ๋“œ๋ฅผ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค. ๋ฐ”๋กœ ์ œ๋ชฉ๊ณผ ๊ฐ™์€ Arrays.asList()์™€ List.of() ๋‘ ๊ฐ€์ง€ ์ž…๋‹ˆ๋‹ค. ๋ณธ ํฌ์ŠคํŒ…์—์„œ๋Š” ๋‘ ๋ฉ”์†Œ๋“œ์˜ ์‚ฌ์šฉ ๋ฐฉ๋ฒ•๊ณผ ์ฐจ์ด๋ฅผ ์•Œ์•„๋ณด๊ณ ์ž ํ•ฉ๋‹ˆ๋‹ค.

 

Arrays.asList()

์ด ๋ฉ”์†Œ๋“œ๋Š” List, ์ฆ‰ Java Collections Framework์— ์†ํ•˜๋Š” List ๊ฐ์ฒด๋ฅผ ๊ฐ„๋‹จํžˆ ์ƒ์„ฑํ•  ์ˆ˜ ์žˆ๊ฒŒ ํ•ด์ค๋‹ˆ๋‹ค. Array(๋ฐฐ์—ด)์„ ์ž…๋ ฅ์œผ๋กœ ๋ฐ›์•„ List ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ฐธ๊ณ ๋กœ JAVA 1.2์—์„œ ๋„์ž…๋œ ์˜ค๋ž˜๋œ ๋ฉ”์†Œ๋“œ์ž…๋‹ˆ๋‹ค.

 

์‚ฌ์šฉ ๋ฐฉ๋ฒ•

๐Ÿ“„ [Excample Code]

@DisplayName("Arrays.asList() ์‚ฌ์šฉ๋ฐฉ๋ฒ•")
@Test
void usageOfArraysAsList() {
    Integer[] array = new Integer[]{1, 2, 3, 4};
    List<Integer> list = Arrays.asList(array);
    assertThat(list).containsExactly(1, 2, 3, 4);
}

 

ํŠน์ง•

Arrays.asList()์˜ ํŠน์ง•์€ ๋‹ค์Œ 3๊ฐ€์ง€๋กœ ์ •๋ฆฌํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

* ๊ณ ์ •๋œ ๋ฆฌ์ŠคํŠธ๋กœ์จ add/remove ์‹œ UnsupportedOperationException ๋ฐœ์ƒ
* ์ž…๋ ฅ๋œ ์›๋ณธ array๋ฅผ List ์ธํ„ฐํŽ˜์ด์Šค๋กœ ๊ฐ์Œ€ ๋ฟ์ด๊ธฐ ๋•Œ๋ฌธ์— ๊ธฐ์กด array์˜ ๋ณ€๊ฒฝ์ด list์—๋„ ๋ฐ˜์˜๋จ
* ๊ฐ€๋ณ€ ๋ฆฌ์ŠคํŠธ(Mutable list)๋ฅผ ๋ฐ˜ํ™˜

 

๊ฐ ํŠน์ง•์„ ์˜ˆ์ œ ์ฝ”๋“œ์™€ ํ•จ๊ป˜ ์ž์„ธํžˆ ์•Œ์•„๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.

 

๊ณ ์ •๋œ ์‚ฌ์ด์ฆˆ -> UnsupportedOperationException ๋ฐœ์ƒ ๊ฐ€๋Šฅ

Arrays.asList() ๋ฉ”์†Œ๋“œ๋Š” '๊ณ ์ •๋œ ์‚ฌ์ด์ฆˆ์˜ ๋ฆฌ์ŠคํŠธ(fixed-size list)'๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ ์ƒˆ element๋ฅผ addํ•˜๊ฑฐ๋‚˜ removeํ•˜๋ ค๊ณ  ํ•˜๋ฉด 'UnsupportedOperationException'์ด ๋ฐœ์ƒํ•ฉ๋‹ˆ๋‹ค.

๐Ÿ“„ [Excample Code]

@DisplayName("Arrays.asList()๋Š” fixed list๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋ฏ€๋กœ add/remove ์‹œ ์˜ˆ์™ธ ๋ฐœ์ƒ")
@Test
void fixedSizeList() {
    List<Integer> list1 = Arrays.asList(1, 2, 3, 4, 5);
    assertThrows(UnsupportedOperationException.class, () -> list1.add(6));

    List<Integer> list2 = Arrays.asList(1, 2, 3, 4, 5);
    assertThrows(UnsupportedOperationException.class, () -> list2.remove(1));
}

 

์›๋ณธ array ๋ณ€๊ฒฝ ์‹œ list์—๋„ ๋ณ€๊ฒฝ์ด ๋ฐ˜์˜๋  ์ˆ˜ ์žˆ์Œ

์ž…๋ ฅ๋œ ์›๋ณธ array๋ฅผ List ์ธํ„ฐํŽ˜์ด์Šค๋กœ ๊ฐ์Œ€ ๋ฟ์ด๊ธฐ ๋•Œ๋ฌธ์— ๊ธฐ์กด array์˜ ๋ณ€๊ฒฝ์ด list์—๋„ ๋ฐ˜์˜๋  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

๐Ÿ“„ [Excample Code]

@DisplayName("์›๋ณธ ๋ฐฐ์—ด ๋ณ€๊ฒฝ์ด list์—๋„ ๋ฐ˜์˜๋จ")
@Test
void changeArrayReflectToList() {
    Integer[] integerArray = new Integer[]{1, 2, 3};
    List<Integer> list1 = Arrays.asList(integerArray);
    integerArray[0] = 1000;
    assertThat(list1.get(0)).isEqualTo(1000);
}

 

๊ฐ€๋ณ€ ๋ฆฌ์ŠคํŠธ(Mutable list)

Arrays.asList()๋กœ ์ƒ์„ฑ๋œ ๋ฆฌ์ŠคํŠธ๋Š” ๊ฐ€๋ณ€์ (mutable)์ž…๋‹ˆ๋‹ค. ์ฆ‰ list ์•ˆ์˜ ๊ฐ element๋Š” ๋ณ€๊ฒฝ๋  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

๐Ÿ“„ [Excample Code]

@DisplayName("Mutable list์ด๋ฏ€๋กœ list์˜ ๊ฐ ์š”์†Œ๋ฅผ ๋ณ€๊ฒฝํ•  ์ˆ˜ ์žˆ๊ณ , ๋ณ€๊ฒฝ์ด ์›๋ณธ array์— ๋ฐ˜์˜๋จ")
@Test
void mutableList() {
    // ์ƒ์„ฑ๋œ list์˜ ๊ฐ element๋ฅผ ๋ณ€๊ฒฝํ•  ์ˆ˜ ์žˆ๋‹ค.
    List<Integer> list1 = Arrays.asList(1, 2, 3, 4);
    list1.set(1, 1000);
    assertThat(list1.get(1)).isEqualTo(1000);

    // ์ƒ์„ฑ๋œ list์˜ ๋ณ€๊ฒฝ์ด ์›๋ณธ array์— ๋ฐ˜์˜๋œ๋‹ค.
    Integer[] array = new Integer[]{1, 2, 3};
    List<Integer> list2 = Arrays.asList(array);
    list2.set(0,1000);
    assertThat(array[0]).isEqualTo(1000);
}

 

List.of()

List.of()๋Š” JAVA 9์—์„œ ๋„์ž…๋œ ๋ฉ”์†Œ๋“œ์ž…๋‹ˆ๋‹ค. Arrays.of()์™€๋Š” ๋Œ€์กฐ์ ์œผ๋กœ, ์ด ๋ฉ”์†Œ๋“œ๋Š” ๋ณ€๊ฒฝ๋ถˆ๊ฐ€ํ•œ(unmodifiable) list ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค.

 

์‚ฌ์šฉ ๋ฐฉ๋ฒ•

๐Ÿ“„ [Excample Code]

@DisplayName("List.of() ์‚ฌ์šฉ๋ฐฉ๋ฒ•")
@Test
void usageOfListOf() {
    Integer[] array = new Integer[]{1, 2, 3};
    List<Integer> list = List.of(array);
    assertThat(list).containsExactly(1, 2, 3)
}

 

Arrays.asList()์™€์˜ ์ฐจ์ด์ 

Arrays.asList()์™€ ๋Œ€์กฐ๋˜๋Š” List.of()์˜ ํŠน์ง•์€ ๋‹ค์Œ 2๊ฐ€์ง€๋กœ ์ •๋ฆฌํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

* ์ž…๋ ฅ๋œ array์— ๋Œ€ํ•ด ๋ถˆ๋ณ€์˜(immutable) list๋ฅผ ๋ฐ˜ํ™˜ -> ์›๋ณธ array์˜ ๋ณ€๊ฒฝ์ด ์ƒ์„ฑ๋œ list์— ๋ฐ˜์˜๋˜์ง€ ์•Š์Œ
* ์ธํ’‹์œผ๋กœ null์ด ํ—ˆ์šฉ๋˜์ง€ ์•Š์Œ(์ž…๋ ฅ ์‹œ NullPointerException ๋ฐœ์ƒ)

 

๋ถˆ๋ณ€ ๋ฆฌ์ŠคํŠธ(Immutable List)

์ฃผ์š” ์ฐจ์ด์ ์€ List.of()๋Š” ์ž…๋ ฅ๋œ array์— ๋Œ€ํ•ด ๋ถˆ๋ณ€์˜(immutable) list๋ฅผ ๋ฐ˜ํ™˜ํ•˜์—ฌ, ์›๋ณธ array์˜ ๋ณ€๊ฒฝ์ด ์ƒ์„ฑ๋œ list์— ๋ฐ˜์˜๋˜์ง€ ์•Š๋Š”๋‹ค๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. ๋˜ํ•œ List.of()๋กœ ์ƒ์„ฑ๋œ list์˜ ๊ฐ element๋Š” ๋ณ€๊ฒฝ๋  ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค. ๋ณ€๊ฒฝํ•˜๋ คํ•˜๋ฉด UnsupportedOperationException์ด ๋ฐœ์ƒํ•ฉ๋‹ˆ๋‹ค.

๐Ÿ“„ [Excample Code]

@DisplayName("Immutable List -> ์›๋ณธ array์˜ ๋ณ€๊ฒฝ์ด list์— ๋ฐ˜์˜๋˜์ง€ ์•Š๊ณ , list์˜ ๊ฐ ์š”์†Œ ๋ณ€๊ฒฝ ์‹œ ์˜ˆ์™ธ ๋ฐœ์ƒ")
@Test
void immutableList() {
    Integer[] array = new Integer[]{1, 2, 3};
    List<Integer> list = List.of(array);
    array[0] = 1000;
    assertThat(list.get(0)).isEqualTo(1);
    assertThrows(UnsupportedOperationException.class, () -> list.set(1, 4));
}

 

Null

List.of()๋Š” null ๊ฐ’์„ ์ž…๋ ฅ์œผ๋กœ ํ—ˆ์šฉํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. null์ด ์ž…๋ ฅ๋˜๋ฉด NullPointerException์ด ๋ฐœ์ƒํ•ฉ๋‹ˆ๋‹ค.

๐Ÿ“„ [Excample Code]

@DisplayName("null ๊ฐ’์„ ํ—ˆ์šฉํ•˜์ง€ ์•Š์Œ")
@Test
void notAllowNullValue() {
    assertThrows(NullPointerException.class, () -> List.of(1, null, 3));
}

 

๊ฒฐ๋ก  - ๊ฐ€๋Šฅํ•˜๋ฉด List.of()๋ฅผ ์‚ฌ์šฉํ•˜์ž

๊ฒฐ๋ก ์ ์œผ๋กœ Arrays.asList()๋Š” Array๋กœ๋ถ€ํ„ฐ ์ƒ์„ฑ๋œ list ์•ˆ์˜ element์˜ ๋ณ€๊ฒฝ์ด ์›๋ณธ array์—๋„ ๋ฐ˜์˜๋  ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ์ฐพ๊ธฐ ์–ด๋ ค์šด ๋ฒ„๊ทธ๋ฅผ ๋ฐœ์ƒ์‹œํ‚ค๋Š” side effects๋ฅผ ์ดˆ๋ž˜ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ JAVA 9 ์ด์ƒ์„ ์‚ฌ์šฉํ•˜๋Š” ํ™˜๊ฒฝ์ด๋ผ๋ฉด ๋˜๋„๋ก List.of()๋ฅผ ์‚ฌ์šฉํ•˜๋„๋ก ํ•˜๊ณ , ๋‘ ๋ฉ”์†Œ๋“œ์˜ ์ฐจ์ด์ ๋„ ํ™•์‹คํžˆ ์•Œ๊ณ  ์‚ฌ์šฉํ•ด์•ผ๊ฒ ์Šต๋‹ˆ๋‹ค.

 

๐Ÿ“„ [์ „์ฒด ์ฝ”๋“œ]

๋”๋ณด๊ธฐ
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;

import java.util.Arrays;
import java.util.List;

import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertThrows;

class MainTest {

    @DisplayName("Arrays.asList() ์‚ฌ์šฉ๋ฐฉ๋ฒ•")
    @Test
    void usageOfArraysAsList() {
        Integer[] array = new Integer[]{1, 2, 3, 4};
        List<Integer> list = Arrays.asList(array);
        assertThat(list).containsExactly(1, 2, 3, 4);
    }

    @DisplayName("Arrays.asList()๋Š” fixed list๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋ฏ€๋กœ add/remove ์‹œ ์˜ˆ์™ธ ๋ฐœ์ƒ")
    @Test
    void fixedSizeList() {
        List<Integer> list1 = Arrays.asList(1, 2, 3, 4, 5);
        assertThrows(UnsupportedOperationException.class, () -> list1.add(6));

        List<Integer> list2 = Arrays.asList(1, 2, 3, 4, 5);
        assertThrows(UnsupportedOperationException.class, () -> list2.remove(1));
    }

    @DisplayName("์›๋ณธ ๋ฐฐ์—ด ๋ณ€๊ฒฝ์ด list์—๋„ ๋ฐ˜์˜๋จ")
    @Test
    void changeArrayReflectToList() {
        Integer[] integerArray = new Integer[]{1, 2, 3};
        List<Integer> list1 = Arrays.asList(integerArray);
        integerArray[0] = 1000;
        assertThat(list1.get(0)).isEqualTo(1000);
    }

    @DisplayName("Mutable list์ด๋ฏ€๋กœ list์˜ ๊ฐ ์š”์†Œ๋ฅผ ๋ณ€๊ฒฝํ•  ์ˆ˜ ์žˆ๊ณ , ๋ณ€๊ฒฝ์ด ์›๋ณธ array์— ๋ฐ˜์˜๋จ")
    @Test
    void mutableList() {
        // ์ƒ์„ฑ๋œ list์˜ ๊ฐ element๋ฅผ ๋ณ€๊ฒฝํ•  ์ˆ˜ ์žˆ๋‹ค.
        List<Integer> list1 = Arrays.asList(1, 2, 3, 4);
        list1.set(1, 1000);
        assertThat(list1.get(1)).isEqualTo(1000);

        // ์ƒ์„ฑ๋œ list์˜ ๋ณ€๊ฒฝ์ด ์›๋ณธ array์— ๋ฐ˜์˜๋œ๋‹ค.
        Integer[] array = new Integer[]{1, 2, 3};
        List<Integer> list2 = Arrays.asList(array);
        list2.set(0,1000);
        assertThat(array[0]).isEqualTo(1000);
    }

    @DisplayName("List.of() ์‚ฌ์šฉ๋ฐฉ๋ฒ•")
    @Test
    void usageOfListOf() {
        Integer[] array = new Integer[]{1, 2, 3};
        List<Integer> list = List.of(array);
        assertThat(list).containsExactly(1, 2, 3);
    }

    @DisplayName("Immutable List -> ์›๋ณธ array์˜ ๋ณ€๊ฒฝ์ด list์— ๋ฐ˜์˜๋˜์ง€ ์•Š๊ณ , list์˜ ๊ฐ ์š”์†Œ ๋ณ€๊ฒฝ ์‹œ ์˜ˆ์™ธ ๋ฐœ์ƒ")
    @Test
    void immutableList() {
        Integer[] array = new Integer[]{1, 2, 3};
        List<Integer> list = List.of(array);
        array[0] = 1000;
        assertThat(list.get(0)).isEqualTo(1);
        assertThrows(UnsupportedOperationException.class, () -> list.set(1, 4));
    }

    @DisplayName("null ๊ฐ’์„ ํ—ˆ์šฉํ•˜์ง€ ์•Š์Œ")
    @Test
    void notAllowNullValue() {
        assertThrows(NullPointerException.class, () -> List.of(1, null, 3));
    }
}

 

References

https://www.baeldung.com/java-arrays-aslist-vs-list-of

 

๋ฐ˜์‘ํ˜•

๋Œ“๊ธ€