<span class="hljs-function"><span class="hljs-keyword">fun</span> <span class="hljs-type"><T></span> Iterable<span class="hljs-type"><T></span>.<span class="hljs-title">first</span><span class="hljs-params">(predicate: (<span class="hljs-type">T</span>) -> <span class="hljs-type">Boolean</span>)</span></span>: T</pre></div><div id="a2d9"><pre><span class="hljs-keyword">inline</span> <span class="hljs-function"><span class="hljs-keyword">fun</span> <span class="hljs-type"><T></span> Iterable<span class="hljs-type"><T></span>.<span class="hljs-title">last</span><span class="hljs-params">(predicate: (<span class="hljs-type">T</span>) -> <span class="hljs-type">Boolean</span>)</span></span>: T</pre></div><p id="0fd4"><b>Example:</b></p>
<figure id="6875">
<div>
<div>
<img class="ratio" src="http://placehold.it/16x9">
<iframe class="" src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fpl.kotl.in%2FlpPpfuHtd&display_name=Kotlin+Playground&url=https%3A%2F%2Fpl.kotl.in%2FlpPpfuHtd&key=a19fcc184b9711e1b4764040d3dc5c07&type=text%2Fhtml&schema=kotl" allowfullscreen="" frameborder="0" height="300" width="800">
</div>
</div>
</figure></iframe></div></div></figure><p id="0f83">The <code>*OrNull</code> variants return <code>null</code> instead of throwing when run on an empty list.</p><div id="669d"><pre><span class="hljs-function"><span class="hljs-keyword">fun</span> <span class="hljs-type"><T></span> List<span class="hljs-type"><T></span>.<span class="hljs-title">firstOrNull</span><span class="hljs-params">()</span></span>: T?</pre></div><div id="0823"><pre><span class="hljs-function"><span class="hljs-keyword">fun</span> <span class="hljs-type"><T></span> List<span class="hljs-type"><T></span>.<span class="hljs-title">lastOrNull</span><span class="hljs-params">()</span></span>: T?</pre></div><div id="b0ac"><pre><span class="hljs-keyword">inline</span> <span class="hljs-function"><span class="hljs-keyword">fun</span> <span class="hljs-type"><T></span> Iterable<span class="hljs-type"><T></span>.<span class="hljs-title">firstOrNull</span><span class="hljs-params">(predicate: (<span class="hljs-type">T</span>) -> <span class="hljs-type">Boolean</span>)</span></span>: T</pre></div><div id="47da"><pre><span class="hljs-keyword">inline</span> <span class="hljs-function"><span class="hljs-keyword">fun</span> <span class="hljs-type"><T></span> Iterable<span class="hljs-type"><T></span>.<span class="hljs-title">lastOrNull</span><span class="hljs-params">(predicate: (<span class="hljs-type">T</span>) -> <span class="hljs-type">Boolean</span>)</span></span>: T</pre></div><p id="5ba5">Finally, <code>firstNotNullOf</code> and <code>firstNotNullOfOrNull</code> apply a transformation to each element, and return the first result which is not null. The former throws <code>NoSuchElementException</code> if no such element is found, while the latter returns <code>null</code>.</p><div id="45c0"><pre><span class="hljs-keyword">inline</span> <span class="hljs-function"><span class="hljs-keyword">fun</span> <span class="hljs-type"><T, R : Any></span> Iterable<span class="hljs-type"><T></span>.<span class="hljs-title">firstNotNullOf</span><span class="hljs-params">(
transform: (<span class="hljs-type">T</span>) -> <span class="hljs-type">R</span>?
)</span></span>: R</pre></div><div id="e8ef"><pre><span class="hljs-keyword">inline</span> <span class="hljs-function"><span class="hljs-keyword">fun</span> <span class="hljs-type"><T, R : Any></span> Iterable<span class="hljs-type"><T></span>.<span class="hljs-title">firstNotNullOfOrNull</span><span class="hljs-params">(
transform: (<span class="hljs-type">T</span>) -> <span class="hljs-type">R</span>?
)</span></span>: R?</pre></div><p id="e85d"><b>Example</b></p>
<figure id="aafb">
<div>
<div>
<img class="ratio" src="http://placehold.it/16x9">
<iframe class="" src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fpl.kotl.in%2FD-BVGwsDv&display_name=Kotlin+Playground&url=https%3A%2F%2Fpl.kotl.in%2FD-BVGwsDv&key=a19fcc184b9711e1b4764040d3dc5c07&type=text%2Fhtml&schema=kotl" allowfullscreen="" frameborder="0" height="300" width="800">
</div>
</div>
</figure></iframe></div></div></figure><h2 id="93a2">indexOf, lastIndexOf</h2><div id="8bb6"><pre><span class="hljs-function"><span class="hljs-keyword">fun</span> <span class="hljs-type"><T></span> Iterable<span class="hljs-type"><T></span>.<span class="hljs-title">indexOf</span><span class="hljs-params">(element: <span class="hljs-type">T</span>)</span></span>: <span class="hljs-built_in">Int</span></pre></div><div id="d88c"><pre><span class="hljs-function"><span class="hljs-keyword">fun</span> <span class="hljs-type"><T></span> Iterable<span class="hljs-type"><T></span>.<span class="hljs-title">lastIndexOf</span><span class="hljs-params">(element: <span class="hljs-type">T</span>)</span></span>: <span class="hljs-built_in">Int</span></pre></div><p id="64d5">Returns the index of the first/last occurrence of <code>element</code>, or <code>-1</code> if no such element is found.</p>
<figure id="a14b">
<div>
<div>
<img class="ratio" src="http://placehold.it/16x9">
Options
<iframe class="" src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fpl.kotl.in%2FTtQqvbztB&display_name=Kotlin+Playground&url=https%3A%2F%2Fpl.kotl.in%2FTtQqvbztB&key=a19fcc184b9711e1b4764040d3dc5c07&type=text%2Fhtml&schema=kotl" allowfullscreen="" frameborder="0" height="300" width="800">
</div>
</div>
</figure></iframe></div></div></figure><p id="ca99">The related <code>indexOfFirst</code>/<code>indexOfLast</code> methods accepts a predicate and return the index of the first/last element that satisfies it.</p><div id="41bf"><pre><span class="hljs-keyword">inline</span> <span class="hljs-function"><span class="hljs-keyword">fun</span> <span class="hljs-type"><T></span> Iterable<span class="hljs-type"><T></span>.<span class="hljs-title">indexOfFirst</span><span class="hljs-params">(
predicate: (<span class="hljs-type">T</span>) -> <span class="hljs-type">Boolean</span>
)</span></span>: <span class="hljs-built_in">Int</span></pre></div><div id="0209"><pre><span class="hljs-keyword">inline</span> <span class="hljs-function"><span class="hljs-keyword">fun</span> <span class="hljs-type"><T></span> Iterable<span class="hljs-type"><T></span>.<span class="hljs-title">indexOfLast</span><span class="hljs-params">(
predicate: (<span class="hljs-type">T</span>) -> <span class="hljs-type">Boolean</span>
)</span></span>: <span class="hljs-built_in">Int</span></pre></div><p id="de1b"><b>Example</b></p>
<figure id="1d5b">
<div>
<div>
<img class="ratio" src="http://placehold.it/16x9">
<iframe class="" src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fpl.kotl.in%2FGlfRHWaUV&display_name=Kotlin+Playground&url=https%3A%2F%2Fpl.kotl.in%2FGlfRHWaUV&key=a19fcc184b9711e1b4764040d3dc5c07&type=text%2Fhtml&schema=kotl" allowfullscreen="" frameborder="0" height="300" width="800">
</div>
</div>
</figure></iframe></div></div></figure><h2 id="5e0a">single</h2><div id="140b"><pre><span class="hljs-function"><span class="hljs-keyword">fun</span> <span class="hljs-type"><T></span> Iterable<span class="hljs-type"><T></span>.<span class="hljs-title">single</span><span class="hljs-params">()</span></span>: T</pre></div><p id="e4d4">An interesting function that returns the single element of a list if it only contains a single element, and throws otherwise.</p><p id="fa04"><b>Example</b></p>
<figure id="8a7b">
<div>
<div>
<img class="ratio" src="http://placehold.it/16x9">
<iframe class="" src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fpl.kotl.in%2FkDYTBOjuc&display_name=Kotlin+Playground&url=https%3A%2F%2Fpl.kotl.in%2FkDYTBOjuc&key=a19fcc184b9711e1b4764040d3dc5c07&type=text%2Fhtml&schema=kotl" allowfullscreen="" frameborder="0" height="300" width="800">
</div>
</div>
</figure></iframe></div></div></figure><p id="8b28">There are a few variations:</p><p id="2990">One variation accepts a predicate, and returns the element satisfying it if there is precisely one, and throws otherwise.</p><div id="c63f"><pre><span class="hljs-function"><span class="hljs-keyword">fun</span> <span class="hljs-type"><T></span> Iterable<span class="hljs-type"><T></span>.<span class="hljs-title">single</span><span class="hljs-params">(predicate: (<span class="hljs-type">T</span>) -> <span class="hljs-type">Boolean</span>)</span></span>: T</pre></div><p id="566e"><b>Example</b></p>
<figure id="be77">
<div>
<div>
<img class="ratio" src="http://placehold.it/16x9">
<iframe class="" src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fpl.kotl.in%2FhsXsVCll0&display_name=Kotlin+Playground&url=https%3A%2F%2Fpl.kotl.in%2FhsXsVCll0&key=a19fcc184b9711e1b4764040d3dc5c07&type=text%2Fhtml&schema=kotl" allowfullscreen="" frameborder="0" height="300" width="800">
</div>
</div>
</figure></iframe></div></div></figure><p id="d702">There are also <code>*OrNull</code> variants for both of the above, which return <code>null</code> instead of throwing.</p><div id="2c08"><pre><span class="hljs-function"><span class="hljs-keyword">fun</span> <span class="hljs-type"><T></span> Iterable<span class="hljs-type"><T></span>.<span class="hljs-title">singleOrNull</span><span class="hljs-params">()</span></span>: T?</pre></div><div id="8f20"><pre><span class="hljs-function"><span class="hljs-keyword">fun</span> <span class="hljs-type"><T></span> Iterable<span class="hljs-type"><T></span>.<span class="hljs-title">singleOrNull</span><span class="hljs-params">(predicate: (<span class="hljs-type">T</span>) -> <span class="hljs-type">Boolean</span>)</span></span>: T?</pre></div><p id="0b92">Go back to <a href="https://readmedium.com/collection-operations-filtering-55718cf0d741">Collection Operations: Filtering</a>, jump to the <a href="https://readmedium.com/table-of-contents-c52573cfa291">Table of Contents</a>, or continue to <a href="https://readmedium.com/collection-operations-predicates-4fb43d5ec524">Collection Operations: Predicates</a>.</p><figure id="8ecd"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*biBSB579iezsNvEQ_NMLBg.png"><figcaption></figcaption></figure></article></body>
Collection Operations: Single Element Access
An introduction to the most important functions for single element access: find, first, last, indexOf, lastIndexOf, single, and their variants.
— — — — — — — — — — — — — — —
THE CURRENT VERSION OF THIS ARTICLE IS PUBLISHED HERE.
This article is part of the Kotlin Primer, an opinionated guide to the Kotlin language, which is indented to help facilitate Kotlin adoption inside Java-centric organizations. It was originally written as an organizational learning resource for Etnetera a.s. and I would like to express my sincere gratitude for their support.
The first/last functions return, as you would expect, the first/last elements of a list. When the list is empty, they throw a NoSuchElementException.
Example
There are a couple of useful variants.
One variant accepts a predicate, and returns the first/last element satisfying that predicate, or throws NoSuchElementException if no such element is found.
inlinefun<T> Iterable<T>.first(predicate: (T) -> Boolean): T
inlinefun<T> Iterable<T>.last(predicate: (T) -> Boolean): T
Example:
The *OrNull variants return null instead of throwing when run on an empty list.
fun<T> List<T>.firstOrNull(): T?
fun<T> List<T>.lastOrNull(): T?
inlinefun<T> Iterable<T>.firstOrNull(predicate: (T) -> Boolean): T
inlinefun<T> Iterable<T>.lastOrNull(predicate: (T) -> Boolean): T
Finally, firstNotNullOf and firstNotNullOfOrNull apply a transformation to each element, and return the first result which is not null. The former throws NoSuchElementException if no such element is found, while the latter returns null.
inlinefun<T, R : Any> Iterable<T>.firstNotNullOf(
transform: (T) -> R?
): R