Stream에서 toList

코딩테스트 문제를 로컬에서는 자바 17이지만 백준 환경은 자바 11로 되어 이어서 문제를 푸는데 아래와 같은 문제가 발생하였다.

로컬

List<Integer> loc = Arrays.stream(reader.readLine().split(" "))  
    .map(i -> Integer.parseInt(i)).toList();

자바 11환경에서는 toList 메서드(자바 버전 16에 추가)가 아직 존재하지 않아서 발생하는 문제였다.

변경

List<Integer> loc = Arrays.stream(reader.readLine().split(" "))
          .map(i -> Integer.parseInt(i))
          .collect(Collectors.toList());

collect메서드와 Collectors 라이브러리를 사용해서 위와 같이 조립이 가능하다.