무지성 메모

■ int배열을 반복문을 쓰지 않고 stream을 활용하여 list로 변환하는 방법

배게 2022. 2. 19. 14:16
728x90

import java.util.*;
import java.util.stream.*; (Collectors 쓰려면 명시해줘야함)

 

int d = new int[]{};

List<Integer> list = Arrays.stream(d).sorted().boxed().collect(Collectors.toList());

 

Arrays.stream(d) -> stream 변환

.sorted() -> 배열의 오름차순을 요구하는 문제라서 sorted() 메서드 사용

.boxed() -> primitive타입을 wrapper클래스로 박싱하여 반환한다

.collect() -> 필터링 또는 매핑된 요소들을 새로운 컬렉션에 수집하고 이를 리턴한다.