Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to push data into Java stream regularly? [closed]

Normally one should get data from a database using a stream and then send it to Apache POI using a stream, etc. I mean, data should be in stream format from start to end. However my database codes are not written as stream and they have complicated sources from various different types from databases. Thus I need to query my data source like this getData(int page, int perPage). And then I want to forward the results to the stream. Like this:

for(int i = 0; i < 5000; i++) {
    stream.add(getData(i, 10000));
}

So my question is how can I push data into the stream on the fly without using too much RAM?

like image 522
ilhan Avatar asked May 19 '26 15:05

ilhan


1 Answers

You can just implement an iterator and wrap it in a stream right?

Stream<T> stream = stream(new Iterator<T>() {
    private Iterator<T> currentBatch;
    private int page;

    @Override
    public boolean hasNext() {
        if (currentBatch != null && currentBatch.hasNext()) return true;
        currentBatch = getData(page, BATCH_SIZE).iterator();
        return currentBatch.hasNext();
    }

    @Override
    public T next() {
        if (!hasNext()) throw new NoSuchElementException();
        return currentBatch.next();
    }
});

private static <T> Stream<T> stream(Iterator<T> iterator) {
    return StreamSupport.stream(Spliterators.spliteratorUnknownSize(iterator, UNKNOWN_CHARACTERISTICS), false);
}
like image 80
Dici Avatar answered May 21 '26 04:05

Dici



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!