title: Can I use ArrayList<String>, map or hashmap in loops as expressions or statements to have less repetition on my code?
body: <p>My menu items will be increased a lot so Id like to know can I use Array, map or hashmap in expressions to have less repetition. Now I have</p>
  
  <pre><code>override fun onOptionsItemSelected(item: MenuItem): Boolean {
      // Handle item selection
      var messages: ArrayList&lt;String&gt;
      var timestamps: ArrayList&lt;String&gt;
      var linknames: ArrayList&lt;String&gt;
      var parsedData: ParsedData
  
      return when (item.itemId) {
          dataIdRead[0] -&gt; {
              Thread(Runnable {
                  parsedData = GetMessages(dataCity[0]).getParsedData()
                  timestamps = parsedData.timestamps
                  messages = parsedData.messages
                  linknames = parsedData.linknames
                  runOnUiThread {
                      rv_message_list.adapter =
                          MessageAdapter(messages, timestamps, linknames, this)
                      button.visibility = View.VISIBLE
                  }
              }).start()
              true
          }
  
          dataIdRead[1] -&gt; {
              Thread(Runnable {
                  parsedData = GetMessages(dataCity[1]).getParsedData()
                  timestamps = parsedData.timestamps
                  messages = parsedData.messages
                  linknames = parsedData.linknames
                  runOnUiThread {
                      rv_message_list.adapter =
                          MessageAdapter(messages, timestamps, linknames, this)
                      button.visibility = View.VISIBLE
                  }
              }).start()
              true
          }
          dataIdRead[2] -&gt; {
              Thread(Runnable {
                  parsedData = GetMessages(dataCity[2]).getParsedData()
                  timestamps = parsedData.timestamps
                  messages = parsedData.messages
                  linknames = parsedData.linknames
                  runOnUiThread {
                      rv_message_list.adapter =
                          MessageAdapter(messages, timestamps, linknames, this)
                      button.visibility = View.VISIBLE
                  }
              }).start()
              true
          }
          else -&gt; super.onOptionsItemSelected(item)
  </code></pre>
  
  <p>and</p>
  
  <pre><code>   val dataIdRead = arrayOf(R.id.helsinki, R.id.vantaa, R.id.espoo)
     val dataCity = arrayOf("helsinki", "vantaa", "espoo")
  </code></pre>
  
  <p>Something that would make it to look something like this</p>
  
  <pre><code>return when (item.itemId) {
          *some magic* -&gt; {
              Thread(Runnable {
                  parsedData = GetMessages(*some magic*).getParsedData()
                  timestamps = parsedData.timestamps
                  messages = parsedData.messages
                  linknames = parsedData.linknames
                  runOnUiThread {
                      rv_message_list.adapter =
                          MessageAdapter(messages, timestamps, linknames, this)
                      button.visibility = View.VISIBLE
                  }
              }).start()
              true
          }
          else -&gt; super.onOptionsItemSelected(item)
  </code></pre>
  
  <p>I have tried example forEach but at the beginning line <code>return when (item.itemId) {</code> waits expression and I didnt got far with that.</p>