[Java]Set to Array, Array to Set

Set을 Array로 변환

    @Test
    void setToArray() {
        // given
        Set<Integer> set = new HashSet<>();
        set.add(1);
        set.add(2);
        set.add(3);
        set.add(4);

        // when
        Integer[] array = set.toArray(new Integer[0]);

        // then
        Assertions.assertThat(set)
            .containsExactly(array);
    }

Array를 Set으로 변환

    @Test
    void arrayToSet() {
        // given
        Integer[] array = {1, 2, 3, 4};

        // when
        Set<Integer> set = new HashSet<>(Arrays.asList(array));

        // then
        Assertions.assertThat(set)
            .containsExactly(array);
    }

You may also like...

답글 남기기

이메일 주소는 공개되지 않습니다. 필수 필드는 *로 표시됩니다