Stream API 를 활용한 로또 번호 생성기 코드 리팩터링 #52
Unanswered
corock
asked this question in
CHAPTER 5 스트림 활용 - Q&A
Replies: 1 comment
-
스트림을 사용해 본 샘플 코드입니다. LotteryNumberGeneratorShell.java public class LotteryNumberGeneratorShell {
public static void main(String[] args) {
// TODO: 스트림을 이용하여 1 ~ 45 사이의 로또 번호 6개를 중복 없이 오름차순으로 추출하세요.
LotteryNumberGenerator.generate1().forEach(System.out::println);
LotteryNumberGenerator.generate2().forEach(System.out::println);
}
} LotteryNumberGenerator.java public final class LotteryNumberGenerator {
public static List<Integer> generate1() {
return Stream.generate(() -> new SecureRandom().nextInt(45) + 1)
.distinct()
.limit(6)
.sorted()
.collect(Collectors.toList());
}
public static List<Integer> generate2() {
return new SecureRandom().ints(1, 46)
.distinct()
.limit(6)
.sorted()
.boxed()
.collect(Collectors.toList());
}
public static List<Integer> generate3() {
// STUB: Anything else?
return null;
}
} |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
다음 제시하는 코드 블록은 스트림을 이용해서 외부 반복을 내부 반복으로 바꾸는 활용 편입니다.
LotteryNumberGeneratorShell.java
LotteryNumberGenerator.java
Beta Was this translation helpful? Give feedback.
All reactions