Skip to content

mattsibs/pagination-stream

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

11 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Pagination Stream

Java 8 Lazy Stream For Paginated Result Set.

Java Streams have a great api and provide the ability to lazily define loads an operations. Creating your own lazy loaded stream is easy with the Spliterator<T> interface. This repo is an example of using the interface for loading paginated data.

Usage

In order to use the utility, you need to define a PageFetcher<T> which defines how to load a paged result for a given offset and pagesize.

(Note the current interface for page uses the Spring Data object out of convenience, this will be made generic in the future release so adapters can bridge the utility's requirements)

Example

public class Main {
    
    public static void main(final String... args) {
        ...
        pagedStream(userRepository.pageFetcher(), 10, 100)
            .parallel()
            .map(HeavyLifting::aDifficultCalculation)
            .forEach(notifier::notify);
    }
}

Below is an example of a PageFetcher using spring data

@Repository
public interface UserRepository extends JpaRepository<User, Long> {

    default PageFetcher<User> pageFetcher() {
        return (offset, pageSize) -> {
            PageRequest pageable = PageRequest.of(offset, pageSize);
            return findAll(pageable);
        };
    }
}

About

Java 8 Lazy Stream For Paginated Result Set

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages