Fix Python – Java: Equivalent of Python’s range(int, int)?

Question

Asked By – Nick Heiner

Does Java have an equivalent to Python’s range(int, int) method?

Now we will see solution for issue: Java: Equivalent of Python’s range(int, int)?


Answer

Guava also provides something similar to Python’s range:

Range.closed(1, 5).asSet(DiscreteDomains.integers());

You can also implement a fairly simple iterator to do the same sort of thing using Guava’s AbstractIterator:

return new AbstractIterator<Integer>() {
  int next = getStart();

  @Override protected Integer computeNext() {
    if (isBeyondEnd(next)) {
      return endOfData();
    }
    Integer result = next;
    next = next + getStep();
    return result;
  }
};

This question is answered By – Simon Steele

This answer is collected from stackoverflow and reviewed by FixPython community admins, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0