[Java] IntStream, LongStream의 range() vs rangeClosed()

기록하기·2024년 10월 22일

Java

목록 보기
4/9

한 줄 요약

range(시작 인덱스, 종료 인덱스) → 시작 인덱스부터 종료 인덱스 - 1 까지
rangeClosed(시작 인덱스, 종료 인덱스) → 시작 인덱스 부터 종료 인덱스 까지 (end_index 포함)

예시 및 출력

int[] array1 = IntStream.range(0, 6)
        .toArray();

int[] array2 = IntStream.rangeClosed(0, 6)
        .toArray();
[0, 1, 2, 3, 4, 5]
[0, 1, 2, 3, 4, 5, 6]

내부 코드

public static IntStream range(int startInclusive, int endExclusive) {
    if (startInclusive >= endExclusive) {
        return empty();
    } else {
        return StreamSupport.intStream(
                new Streams.RangeIntSpliterator(startInclusive, endExclusive, false), false);
    }
}

public static IntStream rangeClosed(int startInclusive, int endInclusive) {
    if (startInclusive > endInclusive) {
        return empty();
    } else {
        return StreamSupport.intStream(
                new Streams.RangeIntSpliterator(startInclusive, endInclusive, true), false);
    }
}

0개의 댓글