1. The purpose of this post

This post would demo how to use java 8 streams to do generate range values with java.

2. Environments

  • Java 1.8+

3. The demo

3.1 The old way

    public static void main(String[] args) {
        List<Integer> ints = new ArrayList<>();
        for(int i=0;i<=10;i++) {
            ints.add(i);
        }

        ints.stream().forEach(i-> System.out.print(i+",")); //0,1,2,3,4,5,6,7,8,9,10,
    }

3.2 Use java 8 IntStream

        List<Integer> ints = IntStream.rangeClosed(1,10).boxed().collect(Collectors.toList());

        ints.stream().forEach(i-> System.out.print(i+",")); //0,1,2,3,4,5,6,7,8,9,10,

4. Conclusion

You can get more information by view the java 8 IntStream API document.