Java: Python range equivalent (int, int)?

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

+92
java python
Sep 24 '10 at 19:04
source share
10 answers

Guava also provides something similar to the Python range :

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

You can also implement a fairly simple iterator to do the same using the Guava 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; } }; 
+25
Nov 27 '12 at 17:40
source share

Old question, new answer (for Java 8)

  IntStream.range(0, 10).forEach( n -> { System.out.println(n); } ); 

or using method references:

 IntStream.range(0, 10).forEach(System.out::println); 
+209
Apr 07 '14 at 4:00
source share

I am working on a small Java utils library called Jools and contains a Range class that provides the functions you need (there is a loadable JAR).
The constructors are either Range(int stop) , Range(int start, int stop) , or Range(int start, int stop, int step) (similar to a for loop), and you can either iterate over it using a lazy evaluation, or use the method toList() to explicitly get a list of ranges.

 for (int i : new Range(10)) {...} // i = 0,1,2,3,4,5,6,7,8,9 for (int i : new Range(4,10)) {...} // i = 4,5,6,7,8,9 for (int i : new Range(0,10,2)) {...} // i = 0,2,4,6,8 Range range = new Range(0,10,2); range.toList(); // [0,2,4,6,8] 
+15
Sep 29 '11 at 19:01
source share

Since Guava 15.0, Range.asSet () is deprecated and should be removed in version 16. Instead, use the following:

 ContiguousSet.create(Range.closed(1, 5), DiscreteDomain.integers()); 
+15
Jul 22 '13 at 3:51 on
source share
 public int[] range(int start, int stop) { int[] result = new int[stop-start]; for(int i=0;i<stop-start;i++) result[i] = start+i; return result; } 

Forgive any syntax or style errors; I usually program in C #.

+14
Sep 24 '10 at 19:12
source share

You can use the following code snippet to get a set of integers:

  Set<Integer> iset = IntStream.rangeClosed(1, 5).boxed().collect (Collectors.toSet()); 
+8
Mar 09 '17 at 17:21
source share
 public int[] range(int start, int length) { int[] range = new int[length - start + 1]; for (int i = start; i <= length; i++) { range[i - start] = i; } return range; } 

(Long answer, just to say no)

+5
Sep 24 '10 at 19:10
source share

Groovy nifty The range class can be used with Java, although this, of course, is not like groovy.

+2
Feb 12
source share

The "Functional Java" library allows you to program this way to a limited extent, it has a range () method that creates an instance of fj.data.Array.

Cm:

Similarly, the Totally Lazy library offers a lazy range method: http://code.google.com/p/totallylazy/

+2
Sep 15 '13 at 13:24
source share

If you want to use it in the same way as in the Python loop, Java goes well with the for statement, which makes this structure unnecessary for this purpose.

+1
Sep 24 '10 at 19:12
source share



All Articles