42

Is there a way to create an IntStream for a range of ints?

Like if I wanted to stream values 1 to 1000, I could invoke some IntStream static factory to stream that range?

IntStream.forRange(1, 1000).forEach(//do something...

1 Answer 1

76

Never mind, I don't know why I missed it in the API documentation after reading it several times...

IntStream.range(1,1000)
3
  • 15
    and rangeClosed if you want upper bounds to be inclusive. Feb 29, 2016 at 16:08
  • 4
    And add .boxed() if you want to map them to non-integers: IntStream.rangeClosed(1, 10).boxed().map(i -> new User("username " + i)).collect(toImmutableList())
    – Carl G
    Jul 10, 2020 at 17:59
  • Or .mapToObj(): IntStream.rangeClosed(1,10).mapToObj(i -> new User("username " + i)).toList();
    – mskfisher
    Jun 17 at 14:56

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.