title: Kotlin Collection: indexOfFirst vs find
body: <p>It might be a stupid question, but I am not sure whether to use indexOfFirst() or find() as both &quot;Returns the first element matching the given [predicate]&quot;. The only difference is that one returns -1 and other null. When should I use indexOfFirst() or find(). Is there any advantage of one over other. Consider the following code snippet.</p>
  <pre><code>private val mPersonList = mutableListOf&lt;Person&gt;()
  
  private fun findPerson(person: Person) {
      val position = mPersonList.indexOfFirst { it.name == person.name }
      if (position != -1) {
          doSomethingWithPerson(mPersonList[position])
      }
  }
  
  private fun findPersonWithFind(person: Person) {
      val foundPerson = mPersonList.find { it.name == person.name }
      foundPerson?.let { doSomethingWithPerson(it) }
  }
  
  private fun doSomethingWithPerson(foundPerson: Person) {
      //Do something
  }
  </code></pre>