Lambda expression for supplier to generate IntStre

2019-04-06 13:29发布

问题:

How do I replace the Supplier code here with lambda expression

IntStream inStream = Stream.generate(new Supplier<Integer>() {
    int x= 1;
    @Override
    public Integer get() {
        return x++ ;
    }
}).limit(10).mapToInt(t -> t.intValue());
inStream.forEach(System.out::println);

The output of above piece of code is:

1
2
3
4
5
6
7
8
9
10

回答1:

Something like this if you're bound to use Stream.generate specifically :

IntStream inStream = Stream.generate(new AtomicInteger(1)::getAndIncrement)
        .limit(10)
        .mapToInt(t -> t);
inStream.forEach(System.out::println);

Edit: Using IntStream.generate, you can perform it as

IntStream.generate(new AtomicInteger(1)::getAndIncrement).limit(10);

Note: A better solution in terms of the API design would definitely be to make use of Stream.iterate for such a use case.



回答2:

The Stream::generate is not suitable for this issue. According to the documentation:

This is suitable for generating constant streams, streams of random elements, etc.

  • You might want to use rather IntStream::range:

    IntStream intStream = IntStream.range(1, 11);
    intStream.forEach(System.out::println);
    
  • Another solution might be using the IntStream.iterate where you can control the increment comfortably using the IntUnaryOperator:

    IntStream intStream = IntStream.iterate(1, i -> i+1).limit(10);
    intStream.forEach(System.out::println);
    
  • As said, the Stream::generate is suitable for the constant streams or random elements. The random element might be obtained using the class Random so here you might want to get an increment using AtomicInteger:

    AtomicInteger atomicInteger = new AtomicInteger(1);
    IntStream intStream = Stream.generate(atomicInteger::getAndIncrement).limit(10);
    intStream.forEach(System.out::println);
    


回答3:

Or you can use this

IntStream.iterate(1, i -> i + 1)
 .limit(10)
 .forEach(System.out::println);