Python like range with stepping in pure Java

Sławomir Lenart :
In [1]: range(-100, 100, 20)
Out[1]: [-100, -80, -60, -40, -20, 0, 20, 40, 60, 80]

What's the easiest way to create Array like above using standard libraries of Java instead of writting own function?

There is IntStream.range(-100, 100), but step is hardcoded to 1.


THIS IS NOT A DUPLICATE of Java: Equivalent of Python's range(int, int)?, because I need a step (offset) between numbers and want to use java built-in libraries instead of 3rd-party libraries. I've checked that question and answers before adding my own. The difference is subtle but essential.

user11044402 :

Using IntStream::range should work (for your special step of 20).

IntStream.range(-100, 100).filter(i -> i % 20 == 0);

A general implementation allowing negative steps could look like this:

/**
 * Generate a range of {@code Integer}s as a {@code Stream<Integer>} including
 * the left border and excluding the right border.
 * 
 * @param fromInclusive left border, included
 * @param toExclusive   right border, excluded
 * @param step          the step, can be negative
 * @return the range
 */
public static Stream<Integer> rangeStream(int fromInclusive,
        int toExclusive, int step) {
    // If the step is negative, we generate the stream by reverting all operations.
    // For this we use the sign of the step.
    int sign = step < 0 ? -1 : 1;
    return IntStream.range(sign * fromInclusive, sign * toExclusive)
            .filter(i -> (i - sign * fromInclusive) % (sign * step) == 0)
            .map(i -> sign * i)
            .boxed();
}

See https://gist.github.com/lutzhorn/9338f3c43b249a618285ccb2028cc4b5 for a detailed version.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=308664&siteId=1