(혼) (공) (web)

04.22(복습) - Intstream

만석이 2024. 4. 22. 18:38

 

 

Integer[] nums = {1,2,3,4,5,6,2,3,5,6,2,6,9,2,10};

 

nums배열을 Stream으로 변환하여 stram변수에 담아주었다.

Stream<Integer> stream = Arrays.stream(nums);

 

distinct() : 중복제거 

sorted() : 정렬

limit() : 문자열 제한

stream.distinct().sorted().limit(5).forEach(x->System.out.print(x+" "));

 

Stream타입으로 변수를 1~10까지 정수 생성

IntStream intStream = IntStream.rangeClosed(1, 10);

 

skip() : 3을건너뜀

limit() : 입력받을 문자개수 제한

intStream.skip(3).limit(3).forEach(x->System.out.print(x+" "));

 

 

중복제거후 2의 배수만 출력

IntStream intStream2 = IntStream.of(1,2,3,3,3,3,4,6,2,2,7,8,9,10);

intStream2.distinct().filter(x -> x % 2 == 0).forEach(x -> System.out.print(x + " "));