How to generate an IntStream in decreasing order with Java

2 Answers

0 votes
import java.util.stream.IntStream;
  
public class MyClass
{
    public static void main(String[] args)
    {
        int start = 3;  
        int end = 9;   
 
        IntStream.range(start, end)
                .map(i -> start + (end - 1 - i))
                .forEach(System.out::println);
    }
}
 
 
 
 
 
/*
run:
 
8
7
6
5
4
3
 
*/

 



answered Mar 18, 2023 by avibootz
0 votes
import java.util.stream.IntStream;
  
public class MyClass
{
    public static void main(String[] args)
    {
        int start = 3;  
        int end = 9;   
 
        IntStream.iterate(end - 1, i -> i - 1)
                .limit(end - start)
                .forEach(System.out::println);
    }
}
 
 
 
 
 
/*
run:
 
8
7
6
5
4
3
 
*/

 



answered Mar 18, 2023 by avibootz

Related questions

1 answer 202 views
1 answer 180 views
1 answer 176 views
176 views asked May 22, 2020 by avibootz
1 answer 150 views
1 answer 163 views
163 views asked Sep 28, 2019 by avibootz
...