18

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.

jhyot
  • 3,733
  • 1
  • 27
  • 44

2 Answers2

23

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.

Jakub Ch.
  • 3,577
  • 3
  • 24
  • 43
5

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)

notionquest
  • 37,595
  • 6
  • 111
  • 105
  • If I request a Page size of 10, why would the content retrieved have 50 items? If I request 10, I expect to get 10 or less items. I can see that if I request 10, and there are only 4 items left, `getSize` might return 10, but `getNumberOfElements` the actual number of 4. – jhyot Aug 22 '16 at 11:12
  • Updated my answer. – notionquest Aug 22 '16 at 11:23