title: Kotlin ArrayList<HashSet<data class>> .reduce function not working with subset of ArrayList
body: <p>I have a data class like this:</p>
  
  <pre><code>data class Protein(val id: String, val score: Double, val molw: Double, val spc: Int) {
      override fun hashCode() = id.hashCode()
      override fun equals(other: Any?) = other?.let { id == (it as Protein).id } ?: false
  }
  </code></pre>
  
  <p><a href="https://stackoverflow.com/questions/49891344/java-kotlin-finding-the-intersection-of-multiple-hashsets-by-class-id">As per another question I asked</a>, I now have a function that can reduce an ArrayList of HashSets with Proteins </p>
  
  <pre><code>fun intersection(data: ArrayList&lt;HashSet&lt;Protein&gt;&gt;): HashSet&lt;Protein&gt; {
    return data.reduce { acc, it -&gt; acc.retainAll(it); acc }
  }
  </code></pre>
  
  <p>What I want to do is take a subset of the original ArrayList and return a reduced version. So I tried to do this:</p>
  
  <pre><code>fun intersection(data: ArrayList&lt;HashSet&lt;Protein&gt;&gt;, combination: List&lt;Int&gt;): HashSet&lt;Protein&gt; {
  val newdata = ArrayList&lt;HashSet&lt;Protein&gt;&gt;()
  for (entry in combination) {
      newdata.add(data[entry-1]) }
  return newdata.reduce { acc, it -&gt; acc.retainAll(it); acc } }
  </code></pre>
  
  <p>Combination tells the function which entries to take from the data ArrayList (eg 1,2,4). The size of the newdata ArrayList will always be 3 (checked this too).</p>
  
  <p>When I run the raw data (ArrayList of 6) through the first intersection function, it reduces just fine. When I run the same data through the second function, it returns a map error, saying that the Key is not found. </p>
  
  <p>I have a feeling this has something to do with the override function for the Protein data class, but I can't find anything about how to work with functional programming for reduce... </p>