With Collection
everything is clear, but what about the following:
There is an object
with a count()
method and a getPart(int i)
method. So extracting all objects leads to the following boilerplate code:
List<Part> result = new ArrayList<Part>(); for (int i = 0, i < object.count(), i++) { result.add(object.getPart(i)); } return result.stream();
Is there any standard way to pass just 2 producers: () -> object.count()
and (int i) -> object.getPart(i)
to create a stream? Like this:
SomeUtil.stream(object::count, object::getPart);
Just use Stream. of() to create a stream from a bunch of object references. Besides regular object streams Java 8 ships with special kinds of streams for working with the primitive data types int , long and double . As you might have guessed it's IntStream , LongStream and DoubleStream .
Create a stream from Collection The Java Collection framework provides two methods, stream() and parallelStream() , to create a sequential and parallel stream from any collection, respectively.
The Kafka Streams application consists of a single Java Class that creates a stream from the Kafka Topic.
Try this:
IntStream.range(0, object.count()).mapToObj(object::getPart);
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