Search the element of the ArrayList in RecyclerView?

I have a collection let of the form:
id - the id of the element
message - the message
parent - the parent, can contain 0 or the element id

The task is to find the position of a parent to get the data, if parent > 0

What I came up with at the moment:
1)indexOf
IndexOf is a method that just performs I need search but I just do not understand how to use (and possible?) indexOf in the ArrayList. Of course you can create another ArrayList and search in it and then use the resulting index to get the data from the main collection.

2)Cycle
To iterate over the entire collection cycle and find the correct index

Tell me how to implement it more correctly
June 8th 19 at 16:53
1 answer
June 8th 19 at 16:55
um, do I understand correctly that the parent is in the same collection as the item, and your task is to find the parent in the parent element? What prevents to go over the collection again in search of a parent? indexOf is exactly what is does. Well, it is only natural to search for the parents of n elements will have quadratic complexity
I understand indexOf looking at all the fields I need to search only obj id, can you tell me how to do it? Yes, the parent is in the same collection. - Rowland.Gulgowski12 commented on June 8th 19 at 16:58
he is looking for equals, not across all fields. Sorted list in a loop and check id's for each item until you find the right - Anabelle_Hettinger commented on June 8th 19 at 17:01

Find more questions by tags AndroidJava