Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Java racing car 한대희 #6

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open

Conversation

hdh4952
Copy link

@hdh4952 hdh4952 commented Nov 21, 2022

No description provided.

Comment on lines +20 to +26
List<String> carNames = util.getCarNames();
int carCount = carNames.size();
Car[] cars = new Car[carCount];

for (int i = 0; i < carCount; i++) {
cars[i] = new Car(carNames.get(i));
}
Copy link

@hyuunnn hyuunnn Nov 22, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Car[] cars = util.getCarNames().stream().map(Car::new).toArray(Car[]::new);

Stream을 사용하면 한 줄로 해결할 수 있습니다..!

Comment on lines +37 to +42
int maxMove = 0;
for (Car car : cars) {
if (maxMove < car.getMoveCount()) {
maxMove = car.getMoveCount();
}
}
Copy link

@hyuunnn hyuunnn Nov 22, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

int maxMove = Arrays.stream(cars).map(Car::getMoveCount).reduce(0, Integer::max);

Stream을 사용하였습니다 ㅎㅎ

Comment on lines +44 to +49
ArrayList<String> winners = new ArrayList<>();
for (Car car : cars) {
if (maxMove == car.getMoveCount()) {
winners.add(car.getName());
}
}
Copy link

@hyuunnn hyuunnn Nov 22, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ArrayList<String> winners = Arrays.stream(cars)
        .filter(car -> maxMove == car.getMoveCount())
        .map(Car::getName)
        .collect(Collectors.toCollection(ArrayList::new));

사용하신 코드를 stream을 사용하여 ArrayList로 생성할 수 있네요!

List<String> winners = Arrays.stream(cars)
        .filter(car -> maxMove == car.getMoveCount())
        .map(Car::getName)
        .toList();

코드를 보니 add를 하기 위해서 ArrayList를 사용하신 것 같은데 따로 추가되는 데이터가 없으면 List를 사용하여 toList()로 해주면 코드가 더 간결해지겠네요 :)

Comment on lines +50 to +58
boolean isPrintName = false;
for (String winner : winners) {
if (isPrintName) {
System.out.print(", " + winner);
} else {
System.out.print(winner);
}
isPrintName = true;
}
Copy link

@hyuunnn hyuunnn Nov 22, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

System.out.printf(winners.stream().collect(Collectors.joining(", ")));

Stream을 사용하여 간결하게 코드를 짤 수 있네요 :)
데이터를 저장하는 기능과 출력하는 기능이 따로 있는데, stream을 사용하여 이를 합쳐주면 코드가 더 간결해지겠네요.

System.out.print(Arrays.stream(cars)
        .filter(car -> maxMove == car.getMoveCount())
        .map(Car::getName).collect(Collectors.joining(", ")));

winners 변수에서 사용한 stream 코드를 활용하여 출력하는 방법입니다.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

크... stream 너무 좋네요 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants