Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring Data Slice: difference between getSize() and getNumberOfElements()

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.

like image 708
jhyot Avatar asked Aug 22 '16 10:08

jhyot


People also ask

How does Spring Pageable work?

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.

What is Page <> in Java?

A page is a sublist of a list of objects. It allows gain information about the position of it in the containing entire list.

How do I use PageRequest?

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.

How do I get back a blank page in spring boot?

From Spring Boot 2.0, you can use Page. empty() to return empty Page . Save this answer.


2 Answers

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.

like image 100
Jakub Ch. Avatar answered Oct 24 '22 01:10

Jakub Ch.


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)

like image 5
notionquest Avatar answered Oct 24 '22 01:10

notionquest