Conversation
hyuunnn
reviewed
Nov 22, 2022
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)); | ||
| } |
There was a problem hiding this comment.
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(); | ||
| } | ||
| } |
There was a problem hiding this comment.
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()); | ||
| } | ||
| } |
There was a problem hiding this comment.
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()로 해주면 코드가 더 간결해지겠네요 :)
hyuunnn
reviewed
Nov 22, 2022
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; | ||
| } |
There was a problem hiding this comment.
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 코드를 활용하여 출력하는 방법입니다.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
No description provided.