basically the title.
If I had to find what I know as a single field,
a.any {
it.name == "user"
}
Now I've a listOf(Groups) Which contains a unique ID
I want to check
if user.groups.anyItemInThisList.UNIQUEID == otheruser.groups.anyItemInThisList.UNIQUEID
My data looks like this
{
"groups":[
{
"id":4
"group":"Test Group",
"role":"creator",
"member_count":1,
"userType":"local"
}
]
}
CodePudding user response:
To rephrase your question (making sure I understand correctly), you have two Lists of the same kind of item, and you want to determine if there is any value of the id
property of an item that appears in both lists.
To do this with simple code, but O(n^2) time, you could use this. It iterates all items from a
and for each item it iterates b
to see if there are any matches.
val result = a.any { x1 -> b.any { x2 -> x1.id == x2.id} }
To do it in O(n) you can do it with a Set. This creates a set of the names from the first list, and then it only has to iterate the second list once to see if any of the names are in the first set.
val aIds = a.mapTo(HashSet(a.size)) { it.id }
val result = b.any { it.id in aIds }