[문제]
중복을 제거한 숫자 리스트를 만드세요.
[데이터]
[1, 2, 2, 3, 3, 3, 4, 4, 4, 4, 5]
[요구사항]
- distinct() 메서드로 중복 제거
- 결과 출력
[소스]
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
public class StreamDistinct {
public static void main(String[] args) {
List<Integer> numbers = Arrays.asList(1, 2, 2, 3, 3, 3, 4, 4, 4, 4, 5);
System.out.println("원본: " + numbers);
// distinct()로 중복된 요소 제거
List<Integer> unique = numbers.stream()
.distinct() // 중복 제거
.collect(Collectors.toList());
System.out.println("중복 제거후: " + unique);
}
}
다음검색