DrMaemi / blog

1 stars 0 forks source link

[Java] ArrayList 초기화 방법 4가지 #156

Open DrMaemi opened 11 months ago

DrMaemi commented 11 months ago

1. Arrays.asList("a", "b", "c")

List<String> list = Arrays.asList("a", "b", "c");
List<String> arrList = new ArrayList<>(list);

2. List.of("a", "b", "c")

List<String> list = List.of("a", "b", "c");
List<String> arrList = new ArrayList<>(list);

3. Double brace initialization

ArrayList<String> arrList = new ArrayList<String>() {{
            add("apple");
            add("grape");
            add("banana");
            add("kiwi");
        }};

4. Stream.of(String[])

String[] arr = new String[] { "apple", "grape", "banana", "kiwi" };
ArrayList<String> arrList = new ArrayList<>(Stream.of(arr)
        .collect(Collectors.toList()));

A. 참조