-
Notifications
You must be signed in to change notification settings - Fork 0
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
base: main
Are you sure you want to change the base?
The head ref may contain hidden characters: "java_racing_car_\uD55C\uB300\uD76C"
Conversation
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)); | ||
} |
There was a problem hiding this comment.
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을 사용하면 한 줄로 해결할 수 있습니다..!
int maxMove = 0; | ||
for (Car car : cars) { | ||
if (maxMove < car.getMoveCount()) { | ||
maxMove = car.getMoveCount(); | ||
} | ||
} |
There was a problem hiding this comment.
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을 사용하였습니다 ㅎㅎ
ArrayList<String> winners = new ArrayList<>(); | ||
for (Car car : cars) { | ||
if (maxMove == car.getMoveCount()) { | ||
winners.add(car.getName()); | ||
} | ||
} |
There was a problem hiding this comment.
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()로 해주면 코드가 더 간결해지겠네요 :)
boolean isPrintName = false; | ||
for (String winner : winners) { | ||
if (isPrintName) { | ||
System.out.print(", " + winner); | ||
} else { | ||
System.out.print(winner); | ||
} | ||
isPrintName = true; | ||
} |
There was a problem hiding this comment.
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 코드를 활용하여 출력하는 방법입니다.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
크... stream 너무 좋네요 👍
No description provided.