title: Jsoup: Safe Elements.get(0)
body: <p>The following snippet of code extracts one and only one element, specifically the <em>first</em> element:</p>
  
  <pre><code>  String linkHref = "";
    String linkText = "";
    Elements links = div.getElementsByTag("a");
    for (Element link : links) {
      linkHref = link.attr("href");
      linkText += link.text();              
      break;
    }    
  </code></pre>
  
  <p>This is really cumbersome code compared to the concise <code>links.get(0)</code> but it has one important feature: It will <strong>not</strong> throw an <code>IndexOutOfBoundException</code> if Elements is empty. Instead, it will simply leave the strings empty.</p>
  
  <p>I can encapsulate this into my own function but it's hard for me to believe that Jsoup doesn't have such function already (I prefer using library function over "re-inventing the wheel" as much as possible). I searched the documentation but couldn't find any.</p>
  
  <p>Do you know whether such "safe <code>Elements.get(0)</code>" exists in Jsoup?</p>