Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using a stream to iterate n times instead of using a for loop to create n items

Say I want to create n items. Pre Java 8, I would write:

List<MyClass> list = new ArrayList<>(); for (int i = 0; i < n; i++) {     list.add(new MyClass()); } 

Is there an elegant way to use a stream to create n items?
I thought of this:

List<MyClass> list = Stream.iterate(0, i -> i).limit(10)     .map(o -> new MyClass()).collect(Collectors.toList()); 

Is there a standard/better way of coding this?

Note that the actual usage is a bit more complex and using a stream would be more flexible because I can immediately pump the items through other functions in one line without even creating a reference to the list, for example grouping them:

Stream.iterate(0, i -> i).limit(10).map(o -> new MyClass())     .collect(Collectors.groupingBy(...)); 
like image 944
Bohemian Avatar asked Jan 22 '15 03:01

Bohemian


People also ask

Why is a stream better than a for loop?

Advantages of the streams:Your stream-handling code doesn't need to know the source of the stream or its eventual terminating method. Streams can succinctly express quite sophisticated behavior. Streams can be a replacement for looping because they allow for the processing of a sequence of data (similarly to a loop).

Is stream forEach better than for loop?

forEach() can be implemented to be faster than for-each loop, because the iterable knows the best way to iterate its elements, as opposed to the standard iterator way. So the difference is loop internally or loop externally.

What can I use instead of a for loop in Java?

To answer your question, an alternative would be to use a Set and check that its size is the same as the array length after you inserted all the array's elements into it (or check on the fly). By efficienty you change the complexity of the algorithm from O(n^2) to O(n) but you use (at most) O(n) extra space.


1 Answers

You could use Stream#generate with limit:

Stream.generate(MyClass::new).limit(10); 
like image 70
Alex - GlassEditor.com Avatar answered Oct 05 '22 21:10

Alex - GlassEditor.com