Generate an IntStream in decreasing order in Java
This post will discuss how to generate an IntStream in decreasing order. In other words, generate integers in a specified range from high to low using streams in Java.
1. Using IntStream.range() with map() method
We know that IntStream.range() can generate a sequence of increasing values within the specified range. The idea is to map each value in such a manner that the resulting sequence is decreasing, as shown below:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
import java.util.stream.IntStream; // Generate an `IntStream` in decreasing order in Java class Main { public static void main(String[] args) { int start = 2; // inclusive int end = 5; // exclusive IntStream.range(start, end) .map(i -> start + (end - 1 - i)) .forEach(System.out::println); } } |
Output:
4
3
2
2. Using IntStream.range() + Sorting
Here’s another approach that simply sorts the increasing sequence in reverse order to get the sequence in decreasing order.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
import java.util.Collections; import java.util.stream.IntStream; // Generate an `IntStream` in decreasing order in Java class Main { public static void main(String[] args) { int start = 2; // inclusive int end = 5; // exclusive IntStream.range(start, end) .boxed() .sorted(Collections.reverseOrder()) .forEach(System.out::println); } } |
Output:
4
3
2
3. Using IntStream.iterate() with limit() method
We can also use IntStream.iterate() method to get sequence in decreasing order. It takes two parameters – a starting value and a lambda expression that reduces the previous value by 1.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
import java.util.stream.IntStream; // Generate an `IntStream` in decreasing order in Java class Main { public static void main(String[] args) { int start = 2; // inclusive int end = 5; // exclusive IntStream.iterate(end - 1, i -> i - 1) .limit(end - start) .forEach(System.out::println); } } |
Output:
4
3
2
4. Using IntStream.generate() with AtomicInteger
We can also use IntStream.generate() with AtomicInteger to get an integer counter that is also thread-safe and call the decrementAndGet() method to get the sequence in decreasing order, as shown below:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
import java.util.concurrent.atomic.AtomicInteger; import java.util.stream.IntStream; // Generate an `IntStream` in decreasing order in Java class Main { public static void main(String[] args) { int start = 2; // inclusive int end = 5; // exclusive AtomicInteger counter = new AtomicInteger(end); IntStream.generate(counter::decrementAndGet) .limit(end - start) .forEach(System.out::println); } } |
Output:
4
3
2
5. Using IntStream.generate() with PrimitiveIterator.OfInt() method
We can also use PrimitiveIterator.OfInt with IntStream.generate(), as shown below:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
import java.util.PrimitiveIterator; import java.util.stream.IntStream; // Generate an `IntStream` in decreasing order in Java class Main { public static void main(String[] args) { int start = 2; // inclusive int end = 5; // exclusive PrimitiveIterator.OfInt itr = new PrimitiveIterator.OfInt() { private int counter = end - 1; @Override public int nextInt() { return counter--; } @Override public boolean hasNext() { return false; } }; IntStream.generate(itr::nextInt) .limit(end - start) .forEach(System.out::println); } } |
Output:
4
3
2
That’s all about generating an IntStream in decreasing order in Java.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)