title: Is there an elegant way do collect and convert the type at the same time in Scala
body: <p>I have a list of properties which can put to different categories. The value type differs. But different categories might have the same value type.</p>
  
  <pre><code>val data: List[(String, Any)] = List("first name" -&gt; "rockie", 
                                       "last name" -&gt; "yang",
                                       "address" -&gt; "rather not reveal",
                                       "wanted age" -&gt; 23)
  
  val publicProps = Set("first name", "last name")
  val privateProps = Set("address", "wanted age")
  
  val filtered = data.filter(d =&gt; publicProps.contains(d._1))
  
  // but the type of filtered is List[(String, Any)]
  
  // I could do a map to convert the type to List[(String, String)]
  filtered.map{case (name, value: String) =&gt; name -&gt; value}
  
  // If the number of publicProps is small
  // I could do collect and convert at the same type
  data.collect {
      case ("first name", value: String) =&gt;
          ("first name", value)
      case ("last name", value: String) =&gt;
          ("last name", value)
  }
  </code></pre>
  
  <p>Is there an elegant way do collect and convert the type at the same type?</p>