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(...));
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).
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.
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.
You could use Stream#generate
with limit
:
Stream.generate(MyClass::new).limit(10);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With