I'm having trouble finding the intersection of an Array of Hashed Sets that contain a data Class (where I want to intersect by identifier):
class Protein(val id: String, val score: Double, val molw: Double, val spc: Int)
I've pulled in some data from a .csv file into this type of structure:
ArrayList<HashSet<Protein>>
So I have six array lists [1 for each csv], each containing one hashed set that contains thousands of Protein structures. Here's what I've tried so far to get an intersection HashSet based off of common Protein.id:
fun intersection(data: ArrayList<HashSet<Protein>>): HashSet<Protein> {
val intersectionSet = HashSet<Protein>(data[0])
for (i in 1..data.size) {
intersectionSet.retainAll(data[i])
}
return intersectionSet
}
This returns an empty list, which makes sense given that it's trying to intersect Protein objects and match each criteria as a whole.
How do I call data[i].id as my intersection criteria? I'm fairly new to Kotlin and data classes :)
To simplify the given answer, you can use
apply
:If you add definitions for the
hashCode
andequals
function in theProtein
class as follows, then theHashSet
will be able to appropriately check the intersection using theid
field.Also you probably want to change the range in your loop within the
intersection
function to be1..(data.size-1)
instead of1..data.size
to avoid going out of bounds. Alternatively you could write it functionally as follows: