Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

filter an arraylist dynamically based on a string value- kotlin

I want to filter an arraylist indexes and filter a doc based on the docid I provide.

The below code works for 0th index but cant search the whole list and findout the docid

  var results =
this._items?.userItems?.filter { it -> it.requests.get(0).doc == docid }

Kindly suggest the better way of doing It.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Any help is highly appreciated

Thanks

>Solution :

Assuming that you want to keep only the userItems that have any request with the given docid, you can use it.requests.any { request -> request.doc == docid }.

From the documentation (see the overload that takes in a predicate):

Returns true if at least one entry matches the given predicate.

If you want to get all the requests with the given docid instead, you can use: userItems.flatMap { it.requests }.filter { it.doc == docid }

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading