What is the difference between getSize()
and getNumberOfElements
in the Spring Data class org.springframework.data.domain.Slice
?
The Javadoc does not offer too much help here.
Spring Data REST Pagination In Spring Data, if we need to return a few results from the complete data set, we can use any Pageable repository method, as it will always return a Page. The results will be returned based on the page number, page size, and sorting direction.
A page is a sublist of a list of objects. It allows gain information about the position of it in the containing entire list.
We can create a PageRequest object by passing in the requested page number and the page size. In Spring MVC, we can also choose to obtain the Pageable instance in our controller using Spring Data Web Support. The findAll(Pageable pageable) method by default returns a Page<T> object.
From Spring Boot 2.0, you can use Page. empty() to return empty Page . Save this answer.
getSize() returns the capacity of the Slice.
getNumberOfElements() how many elements does the Slice contains.
For example: You want Page of data from PagingAndSortingRepository. You can call method like repo.findAll(new PageRequest(0,30)) what means you request for first page of data which contains 30 entities at most. Assuming that there are only 10 entities in database you receive a Page where size is 30 and numberOfElements is 10.
Here is the difference.
Consider, for example full content retrieved has 55 items and page size is 10.
getSize - return the Page size (i.e. current page size) if it is pageable
Example: A page can be defined to have 10 items. So, getSize() would return 10 based on Page definition.
getNumberOfElements - returns the actual content size of a page
Example:- The number of elements can be 10 or less than 10 based on the actual data. The last page would return 5 items.
org.springframework.data.domain.AbstractPageRequest.java - has size attribute
org.springframework.data.domain.Chunk - abstract class has the definition for getNumberOfElements() method returning the size of contents (i.e. list type)
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