<p>I've recently been working on a beginner's project in Scala, and have a beginner question about Scala's Lists.</p>

<p>Say I have a list of tuples ( <code>List[Tuple2[String, String]]</code>, for example). Is there a convenience method to return the first occurence of a specified tuple from the List, or is it necessary to iterate through the list by hand?</p>

[{'answer_id': 54123, 'author': 'Tim Sullivan', 'author_id': 722, 'author_profile': 'https://Stackoverflow.com/users/722', 'pm_score': 3, 'selected': False, 'text': '<p>You could try using <a href="http://www.scala-lang.org/api/current/index.html#scala.collection.parallel.ParIterableLike$Find" rel="nofollow noreferrer">find</a>. (Updated scala-doc location of find)</p>\n'}, {'answer_id': 56534, 'author': 'sblundy', 'author_id': 4893, 'author_profile': 'https://Stackoverflow.com/users/4893', 'pm_score': 2, 'selected': False, 'text': '<p>If you\'re learning scala, I\'d take a good look at the <a href="http://www.scala-lang.org/api/current/scala/collection/Seq.html" rel="nofollow noreferrer">Seq</a> trait. It provides the basis for much of scala\'s functional goodness.</p>\n'}, {'answer_id': 61497, 'author': 'Binil Thomas', 'author_id': 3973, 'author_profile': 'https://Stackoverflow.com/users/3973', 'pm_score': 4, 'selected': False, 'text': '<pre>\nscala> val list = List(("A", "B", 1), ("C", "D", 1), ("E", "F", 1), ("C", "D", 2), ("G", "H", 1))\nlist: List[(java.lang.String, java.lang.String, Int)] = List((A,B,1), (C,D,1), (E,F,1), (C,D,2), (G,H,1))\n\nscala> list find {e => e._1 == "C" && e._2 == "D"}\nres0: Option[(java.lang.String, java.lang.String, Int)] = Some((C,D,1))\n</pre>\n'}, {'answer_id': 66489, 'author': 'Daniel Spiewak', 'author_id': 9815, 'author_profile': 'https://Stackoverflow.com/users/9815', 'pm_score': 2, 'selected': False, 'text': '<p>As mentioned in a previous comment, <code>find</code> is probably the easiest way to do this.  There are actually three different "linear search" methods in Scala\'s collections, each returning a slightly different value.  Which one you use depends upon what you need the data for.  For example, do you need an index, or do you just need a boolean <code>true</code>/<code>false</code>?</p>\n'}, {'answer_id': 392351, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 1, 'selected': False, 'text': '<p>You could also do this, which doesn\'t require knowing the field names in the Tuple2 class--it uses pattern matching instead:</p>\n\n<pre><code>list find { case (x,y,_) =&gt; x == "C" &amp;&amp; y == "D" }\n</code></pre>\n\n<p>"find" is good when you know you only need one; if you want to find all matching elements you could either use "filter" or the equivalent sugary for comprehension:</p>\n\n<pre><code>for ( (x,y,z) &lt;- list if x == "C" &amp;&amp; y == "D") yield (x,y,z)\n</code></pre>\n'}, {'answer_id': 13305849, 'author': 'akauppi', 'author_id': 14455, 'author_profile': 'https://Stackoverflow.com/users/14455', 'pm_score': 1, 'selected': False, 'text': '<p>Here\'s code that may help you.</p>\n\n<p>I had a similar case, having a collection of base class entries (here, <code>A</code>) out of which I wanted to find a certain derived class\'s node, if any (here, <code>B</code>).</p>\n\n<pre><code>class A\n\ncase class B(val name: String) extends A\n\nobject TestX extends App {\n  val states: List[A] = List( B("aa"), new A, B("ccc") )\n\n  def findByName( name: String ): Option[B] = {\n    states.find{\n      case x: B if x.name == name =&gt; return Some(x)\n      case _ =&gt; false\n    }\n    None\n  }\n\n  println( findByName("ccc") )    // "Some(B(ccc))"\n}\n</code></pre>\n\n<p>The important part here (for my app) is that <code>findByName</code> does not return <code>Option[A]</code> but <code>Option[B]</code>.</p>\n\n<p>You can easily modify the behaviour to return <code>B</code> instead, and throw an exception if none was found. Hope this helps.</p>\n'}, {'answer_id': 32812857, 'author': 'elm', 'author_id': 3189923, 'author_profile': 'https://Stackoverflow.com/users/3189923', 'pm_score': 1, 'selected': False, 'text': '<p>Consider <code>collectFirst</code> which delivers <code>Some[(String,String)]</code> for the first matching tuple or <code>None</code> otherwise, for instance as follows,</p>\n\n<pre><code>xs collectFirst { case t@(a,_) if a == "existing" =&gt; t }\nSome((existing,str))\n\nscala&gt; xs collectFirst { case t@(a,_) if a == "nonExisting" =&gt; t }\nNone\n</code></pre>\n\n<p>Using <code>@</code> we bind the value of the tuple to <code>t</code> so that a whole matching tuple can be collected.</p>\n'}]
['https://Stackoverflow.com/questions/54010', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/-1/']