Java's 9 equivalent of python's range could be done by using:

  • java.util.stream.IntStream
  • range(0, 5)
  • forEach

Java 9 IntStream range

This is an example printing numbers from 0 to 5

import java.util.stream.IntStream;
IntStream.range(0, 5).forEach(
         n -> {
        System.out.println(n);
     }
);

result:

0
1
2
3
4
5

Java 9 IntStream range

Another way to generate range would be by:

  • import java.util.Set;
  • import java.util.stream.Collectors;
import java.util.Set;
import java.util.stream.Collectors;
Set<Integer> myset = IntStream.rangeClosed(0, 5).boxed().collect(Collectors.toSet());
        System.out.println(myset);

result:

[0, 1, 2, 3, 4, 5]