[우아한 테크코스-프리코스 2주차] Collections, Stream 적용하기

우아한 테크코스 2주차 미션 자동차 경주 게임 GitHub Link

Collections, Stream()


image

지난 1 주차 미션 공통 피드백 사항으로 직접 로직을 구현하기 보다 JAVA 에서 제공하는 API와 Java Collection을 적극적으로 활용하라는 내용이 첨부되어있었다. 그래서 이를 활용하기 위해 미션 해결 과정에서 필요한 Collection과 Stream에 대해 알아보았다.

controller/RacingController.java

    private void inputNames() {
        OutputView.printCarName();
        List<Car> carList = Arrays.stream(InputView.read().split(","))
            .map(Car::new)
            .collect(Collectors.toList());

        this.cars = new Cars(carList);
    }


  • Arrays.stream 배열 스트림

    스트림은 배열 또는 컬렉션 인스턴스를 이용해서 사용할 수 있다.

    입력받은 연속된 자동차들의 이름을 "," 를 기준으로 나누어서 배열을 생성하기 위해 사용


  • Mapping

    map 은 요소들을 하나씩 특정 값으로 변환해준다. stream에 들어가있는 값이 input, 특정 로직을 거친 output이 새로운 stream에 다시 담기는 것을 mapping이라고 한다.

    이름값 리스트를 Car 객체로 mapping 해주기 위해 사용


  • Collectors.toList()

    stream을 통해 작업한 결과를 다시 list로 반환한다.

    생성된 Car 객체들을 다시 list에 담기 위해 사용



model/Cars.java

public void moveCars() {
        carList.forEach(car -> {
            car.forward(Randoms.pickNumberInRange(MIN_NUMBER, MAX_NUMBER));
            OutputView.printRacing(car.getName(), car.getLocation());
        });
        System.out.println();
    }

public List<Car> winner() {
    int maxLocation = maxLocation();

    return carList.stream()
        .filter(car -> car.isMaxLocation(maxLocation))
        .toList();
}

private int maxLocation() {
    return carList.stream()
        .mapToInt(Car::getLocation)
        .max()
        .getAsInt();
}


  • Iterating

    forEach() 는 요소를 순환하며 반복하는 작업을 수행한다.

    carList의 car.forward() 를 수행하기 위해 사용


  • Filtering

    filter() 는 stream 요소들을 하나씩 선발하여 걸러내는 작업을 한다.

    최대 위치 값과 같은 car 객체만 filtering 하기 위해 사용


  • Calculating

    max() 는 stream 요소들 중 최대값을 선별한다.

    Car 객체들의 location 값 중 최대값을 계산하기 위해 사용


  • getAsInt()

    값을 반환하고, 없다면 예외를 던진다.

    Car 객체들의 location 값 중 최대값을 반환하는 과정에서 예외 상황을 처리하기 위해 사용




[참고자료]