STUDY

IntStream(2)

툰잭 2023. 4. 19. 20:10

목차

  1. skip(): IntStream의 처음 몇 개의 요소를 건너뜁니다.
  2. limit(): IntStream의 일부 요소만을 가져옵니다.
  3. findFirst(): IntStream에서 첫 번째 요소를 반환합니다.
  4. findAny(): IntStream에서 아무 요소나 하나 반환합니다.
  5. noneMatch(): IntStream의 모든 요소가 주어진 조건과 일치하지 않는지 확인합니다.
  6. allMatch(): IntStream의 모든 요소가 주어진 조건과 일치하는지 확인합니다.
  7. anyMatch(): IntStream의 어떤 요소가 주어진 조건과 일치하는지 확인합니다.
  8. toArray(): IntStream을 int[] 배열로 변환합니다.
  9. forEach(): IntStream의 모든 요소에 대해 지정된 작업을 수행합니다.
  10. forEachOrdered(): IntStream의 모든 요소에 대해 지정된 작업을 수행합니다. 이때, 요소의 순서는 보장됩니다.

 

1.skip(): IntStream의 처음 몇 개의 요소를 건너뜁니다.

int[] arr = {1, 2, 3, 4, 5};
int[] result = Arrays.stream(arr)
                     .skip(2)
                     .toArray();
System.out.println(Arrays.toString(result));  // [3, 4, 5]

 

2.limit(): IntStream의 일부 요소만을 가져옵니다.

int[] arr = {1, 2, 3, 4, 5};
int[] result = Arrays.stream(arr)
                     .limit(3)
                     .toArray();
System.out.println(Arrays.toString(result));  // [1, 2, 3]

 

3.findFirst(): IntStream에서 첫 번째 요소를 반환합니다.

int[] arr = {1, 2, 3, 4, 5};
int first = Arrays.stream(arr).findFirst().getAsInt();
System.out.println(first);  // 1

 

4.findAny(): IntStream에서 아무 요소나 하나 반환합니다.

int[] arr = {1, 2, 3, 4, 5};
int any = Arrays.stream(arr).findAny().getAsInt();
System.out.println(any);  // 1 or 2 or 3 or 4 or 5 (임의의 값)

 

5.noneMatch(): IntStream의 모든 요소가 주어진 조건과 일치하지 않는지 확인합니다.

int[] arr = {1, 2, 3, 4, 5};
boolean result = Arrays.stream(arr).noneMatch(value -> value > 10);
System.out.println(result);  // true

 

6.allMatch(): IntStream의 모든 요소가 주어진 조건과 일치하는지 확인합니다.

int[] arr = {1, 2, 3, 4, 5};
boolean result = Arrays.stream(arr).allMatch(value -> value > 0);
System.out.println(result);  // true

 

 

7.anyMatch(): IntStream의 어떤 요소가 주어진 조건과 일치하는지 확인합니다.

int[] arr = {1, 2, 3, 4, 5};
boolean result = Arrays.stream(arr).anyMatch(value -> value > 3);
System.out.println(result);  // true

 

8.toArray(): IntStream을 int[] 배열로 변환합니다.

int[] arr = {1, 2, 3, 4, 5};
int[] result = Arrays.stream(arr).toArray();
System.out.println(Arrays.toString(result));  // [1, 2, 3, 4, 5]

 

9.forEach(): IntStream의 모든 요소에 대해 지정된 작업을 수행합니다.

 

int[] arr = {1, 2, 3, 4, 5};
Arrays.stream(arr).forEach(value -> System.out.print(value + " "));  // 1 2 3 4 5

 

10.forEachOrdered(): IntStream의 모든 요소에 대해 지정된 작업을 수행합니다. 이때, 요소의 순서는 보장됩니다.

int[] arr = {5, 4, 3, 2, 1};
Arrays.stream(arr).forEachOrdered(value -> System.out.print(value + " "));  // 5 4 3 2 1