# How to get paged collection with java sdk

**URL:** https://community.cumulocity.com/t/how-to-get-paged-collection-with-java-sdk/1360
**Category:** Forum
**Tags:** cumulocity
**Created:** [October 21, 2022, 3:19pm UTC](https://community.cumulocity.com/t/how-to-get-paged-collection-with-java-sdk/1360 "2022-10-21T15:19:17Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![thekamandam](https://avatars.discourse-cdn.com/v4/letter/t/5e9695/32.png) [@thekamandam](https://community.cumulocity.com/u/thekamandam)
#### Post date: [October 21, 2022, 3:19pm UTC](https://community.cumulocity.com/t/how-to-get-paged-collection-with-java-sdk/1360/1 "2022-10-21T15:19:17Z")

</div>

Hello!

I am using cumulocity java sdk 1011.0.22 for microservice development.

I need to get list of managed object with pagination information. The list is filtered using  
InventoryFilter object.

How I can get it?

When I do

```auto
inventoryApi.getManagedObjectsByFilter(new InventoryFilter().byFragmentType(fragmentType))

```

it returns `ManagedObjectCollection`. Is it possible to get here the amount of pages?

Thanks in advance!

---

<div class="post-metadata">

### Author: ![Harald\_Meyer](https://dub1.discourse-cdn.com/flex005/user_avatar/community.cumulocity.com/harald_meyer/32/1959_2.png) [@Harald\_Meyer](https://community.cumulocity.com/u/Harald_Meyer)
#### Post date: [October 21, 2022, 4:06pm UTC](https://community.cumulocity.com/t/how-to-get-paged-collection-with-java-sdk/1360/2 "2022-10-21T16:06:45Z")

</div>

Hi Boris,

a ManagedObjectCollection is actually a **PageCollectionResource** which has the following methods:

> **[PagedCollectionResource (Cumulocity SDK - Java Client API)](http://resources.cumulocity.com/documentation/javasdk/current/com/cumulocity/sdk/client/PagedCollectionResource.html)**
>
> declaration: package: com.cumulocity.sdk.client, interface: PagedCollectionResource

To get the first page for a pageSize of 100 you can do

```auto
moc.get(100);

```

This returns a **PagedManagedObjectCollectionRepresentation** :

> **[PagedManagedObjectCollectionRepresentation (Cumulocity SDK - Java Client API)](http://resources.cumulocity.com/documentation/javasdk/current/com/cumulocity/sdk/client/inventory/PagedManagedObjectCollectionRepresentation.html)**
>
> declaration: package: com.cumulocity.sdk.client.inventory, class: PagedManagedObjectCollectionRepresentation

which has a methods **getPageStatistics** that returns a **PageStatisticsRepresentation** that contains information about the number of pages:

> **[PageStatisticsRepresentation (Cumulocity SDK - Java Client API)](http://resources.cumulocity.com/documentation/javasdk/current/com/cumulocity/rest/representation/PageStatisticsRepresentation.html)**
>
> declaration: package: com.cumulocity.rest.representation, class: PageStatisticsRepresentation

---

<div class="post-metadata">

### Author: ![thekamandam](https://avatars.discourse-cdn.com/v4/letter/t/5e9695/32.png) [@thekamandam](https://community.cumulocity.com/u/thekamandam)
#### Post date: [October 24, 2022, 9:33am UTC](https://community.cumulocity.com/t/how-to-get-paged-collection-with-java-sdk/1360/3 "2022-10-24T09:33:12Z")

</div>

Hello, Harald!

Many thanks for your answer.  
Could you please tell me then, how I can get second page in such case?

I found the method

```auto
PageStatisticsRepresentation#getPage(BaseCollectionRepresentation collectionRepresentation, int pageNumber, int pageSize)

```

but I can’t understand what should I pass with the first parameter.

---

<div class="post-metadata">

### Author: ![Harald\_Meyer](https://dub1.discourse-cdn.com/flex005/user_avatar/community.cumulocity.com/harald_meyer/32/1959_2.png) [@Harald\_Meyer](https://community.cumulocity.com/u/Harald_Meyer)
#### Post date: [October 24, 2022, 9:50am UTC](https://community.cumulocity.com/t/how-to-get-paged-collection-with-java-sdk/1360/4 "2022-10-24T09:50:56Z")

</div>

Hi Boris,

the first parameter, is the “current” page on which you are operating. A normal workflow would be to call one of get() methods which returns a BaseCollectionRepresentation of the first page and then navigate back and forwards using getNextPage() or getPreviousPage() while using the last fetched page for the first parameter. Or use getPage() to fetch a specific page. In any case you need to invoke get() once to get the first page.

---

<div class="post-metadata">

### Author: ![Kai\_Sieben](https://dub1.discourse-cdn.com/flex005/user_avatar/community.cumulocity.com/kai_sieben/32/5541_2.png) [@Kai\_Sieben](https://community.cumulocity.com/u/Kai_Sieben)
#### Post date: [October 24, 2022, 11:20am UTC](https://community.cumulocity.com/t/how-to-get-paged-collection-with-java-sdk/1360/5 "2022-10-24T11:20:32Z")

</div>

Hi,  
don’t forget the `allPages()` method (of `PagedManagedObjectCollectionRepresentation`) which returns a handy iterable, which will do the paging under the hood.  
Hence you could do the following and don’t need to care about getting the next page. Once it reaches object 101 (in this case with page size 100) it will get the next page for you.

```auto
Iterable<ManagedObjectRepresentation> it = inventoryApi.getManagedObjectsByFilter(new InventoryFilter().byFragmentType(fragmentType)).get(100).allPages();
for(ManagedObjectRepresentation mo : it){
   //do something
}

```

Regards Kai

---

<div class="post-metadata">

### Author: ![system](https://dub1.discourse-cdn.com/flex005/user_avatar/community.cumulocity.com/system/32/31990_2.png) [@system](https://community.cumulocity.com/u/system)
#### Post date: [April 22, 2023, 11:20am UTC](https://community.cumulocity.com/t/how-to-get-paged-collection-with-java-sdk/1360/6 "2023-04-22T11:20:46Z")

</div>

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.
