avatarGabriel Shanahan

Free AI web copilot to create summaries, insights and extended knowledge, download it at here

5496

Abstract

js-keyword">fun</span> <span class="hljs-type"><T : Any></span> Iterable<span class="hljs-type"><T?></span>.<span class="hljs-title">filterNotNull</span><span class="hljs-params">()</span></span>: List<T></pre></div><p id="c8d1"><b>Example</b></p> <figure id="c057"> <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%2FPK89cFwIP&amp;display_name=Kotlin+Playground&amp;url=https%3A%2F%2Fpl.kotl.in%2FPK89cFwIP&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=kotl" allowfullscreen="" frameborder="0" height="300" width="800"> </div> </div> </figure></iframe></div></div></figure><p id="57a8">As with many other collection operations, the <code>*To</code> and <code>*Indexed</code> variants of most of the previous functions are available if you need them.</p><h1 id="2c9e">Subranges</h1><p id="8134">There are many functions that allow you to take a continous subsection of a list according to some criteria.</p><h2 id="a7a4">take, drop</h2><div id="cfde"><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">take</span><span class="hljs-params">(n: <span class="hljs-type">kotlin</span>.<span class="hljs-type">Int</span>)</span></span>: List<T></pre></div><div id="d4af"><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">drop</span><span class="hljs-params">(n: <span class="hljs-type">kotlin</span>.<span class="hljs-type">Int</span>)</span></span>: List<T></pre></div><p id="ef14">The <code>take</code>/<code>drop</code> functions return the first <code>n</code> elements of the list/return the list <i>without</i> the first <code>n</code> elements, respectively.</p><p id="39cf"><b>Example</b></p> <figure id="5ec3"> <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%2FitJeg0STF&amp;display_name=Kotlin+Playground&amp;url=https%3A%2F%2Fpl.kotl.in%2FitJeg0STF&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=kotl" allowfullscreen="" frameborder="0" height="300" width="800"> </div> </div> </figure></iframe></div></div></figure><p id="d011">There are a few useful variants:</p><p id="37e9">One are the <code>*Last</code> variants, which do the same thing, but for the <i>last</i> <code>n</code> elements.</p><div id="5703"><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">takeLast</span><span class="hljs-params">(n: <span class="hljs-type">Int</span>)</span></span>: List<T></pre></div><div id="2431"><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">dropLast</span><span class="hljs-params">(n: <span class="hljs-type">Int</span>)</span></span>: List<T></pre></div><p id="45b5"><b>Example</b></p> <figure id="02e1"> <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%2FPTUacMdqY&amp;display_name=Kotlin+Playground&amp;url=https%3A%2F%2Fpl.kotl.in%2FPTUacMdqY&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=kotl" allowfullscreen="" frameborder="0" height="300" width="800"> </div> </div> </figure></iframe></div></div></figure><p id="d971">Another are the <code>*While</code> variants, which do the same thing, but instead of taking/dropping a fixed amount of elements, they accept a predicate and take/drop elements while that predicate is true:</p><div id="f93e"><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">takeWhile</span><span class="hljs-params">( predicate: (<span class="hljs-type">T</span>) -> <span class="hljs-type">Boolean</span> )</span></span>: List<T></pre></div><div id="0605"><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">dropWhile</span><span class="hljs-params">( predicate: (<span class="hljs-type">T</span>) -> <span class="hljs-type">Boolean</span> )</span></span>: List<T></pre></div><p id="586c"><b>Example</b></p> <figure id="43c2"> <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%2FnSG9babD1&amp;display_name=Kotlin+Playground&

Options

url=https%3A%2F%2Fpl.kotl.in%2FnSG9babD1&key=a19fcc184b9711e1b4764040d3dc5c07&type=text%2Fhtml&schema=kotl" allowfullscreen="" frameborder="0" height="300" width="800"> </div> </div> </figure></iframe></div></div></figure><p id="990a">You also have <code>*LastWhile</code> variants, which are the combination of the previous two.</p><div id="ebcf"><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">takeLastWhile</span><span class="hljs-params">( predicate: (<span class="hljs-type">T</span>) -> <span class="hljs-type">Boolean</span> )</span></span>: List<T></pre></div><div id="bfb6"><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">dropLastWhile</span><span class="hljs-params">( predicate: (<span class="hljs-type">T</span>) -> <span class="hljs-type">Boolean</span> )</span></span>: List<T></pre></div> <figure id="bc5f"> <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%2FtA7l9RAz4&amp;display_name=Kotlin+Playground&amp;url=https%3A%2F%2Fpl.kotl.in%2FtA7l9RAz4&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=kotl" allowfullscreen="" frameborder="0" height="300" width="800"> </div> </div> </figure></iframe></div></div></figure><h2 id="5f8e">slice</h2><div id="fa54"><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">slice</span><span class="hljs-params">(indices: <span class="hljs-type">IntRange</span>)</span></span>: List<T></pre></div><div id="7c66"><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">slice</span><span class="hljs-params">(indices: <span class="hljs-type">Iterable</span><<span class="hljs-type">Int</span>>)</span></span>: List<T></pre></div><p id="06f3">The <code>slice</code> function comes in two variants — one returns all the elements in a certain index range, while the other returns the elements at the specified indices. Both could be implemented in terms of <code>filterIndexed</code>.</p><p id="cf94"><b>Example</b></p> <figure id="d0e8"> <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%2F1PjASXzpH&amp;display_name=Kotlin+Playground&amp;url=https%3A%2F%2Fpl.kotl.in%2F1PjASXzpH&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=kotl" allowfullscreen="" frameborder="0" height="300" width="800"> </div> </div> </figure></iframe></div></div></figure><h1 id="1052">Miscellaneous</h1><h2 id="f545">distinct, distinctBy</h2><div id="2448"><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">distinct</span><span class="hljs-params">()</span></span>: List<T></pre></div><div id="e783"><pre><span class="hljs-keyword">inline</span> <span class="hljs-function"><span class="hljs-keyword">fun</span> <span class="hljs-type"><T, K></span> Iterable<span class="hljs-type"><T></span>.<span class="hljs-title">distinctBy</span><span class="hljs-params">( selector: (<span class="hljs-type">T</span>) -> <span class="hljs-type">K</span> )</span></span>: List<T></pre></div><p id="658a">Returns a <code>List</code> with all duplicate elements removed. The <code>distinctBy</code> variant removes all elements for which <code>selector</code> returns the same value.</p><p id="67a3"><b>Example</b></p> <figure id="6530"> <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%2F0osTKC0lN&amp;display_name=Kotlin+Playground&amp;url=https%3A%2F%2Fpl.kotl.in%2F0osTKC0lN&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=kotl" allowfullscreen="" frameborder="0" height="300" width="800"> </div> </div> </figure></iframe></div></div></figure><p id="6fe2">Go back to <a href="https://readmedium.com/collection-operations-transformations-8b5e1efdf3d8">Collection Operations: Transformations</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-single-element-access-ebdea8fe451c">Collection Operations: Single Element Access</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: Filtering

An introduction to the most important filtering operations: filter, take, drop, slice, distinct, and their variants.

— — — — — — — — — — — — — — —

THE CURRENT VERSION OF THIS ARTICLE IS PUBLISHED HERE.

— — — — — — — — — — — — — — —

Tags: #FYI++

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.

It is recommended to read the Introduction before moving on. Check out the Table of Contents for all articles.

Filtering

filter

inline fun <T> Iterable<T>.filter(
    predicate: (T) -> Boolean
): List<T>

Probably the most ubiquitous of all filtering functions, filter accepts a predicate and returns a List of all the elements for which the predicate returns true.

Example

There are a few useful variants of filter:

One is filterNot, which returns a List of elements for which the predicate returns false.

inline fun <T> Iterable<T>.filterNot(
    predicate: (T) -> Boolean
): List<T>

Example

Another is filterIsInstance, which takes a single generic parameter, and returns all elements that are of that type. It should be noted that this also narrows the type of the returned list.

inline fun <reified R> Iterable<*>.filterIsInstance(): List<R>

Example

A kind of combination of the previous to is filterNotNull(), which returns all non-null elements. Notice how the return type is constrained to a non-nullable one.

fun <T : Any> Iterable<T?>.filterNotNull(): List<T>

Example

As with many other collection operations, the *To and *Indexed variants of most of the previous functions are available if you need them.

Subranges

There are many functions that allow you to take a continous subsection of a list according to some criteria.

take, drop

fun <T> Iterable<T>.take(n: kotlin.Int): List<T>
fun <T> Iterable<T>.drop(n: kotlin.Int): List<T>

The take/drop functions return the first n elements of the list/return the list without the first n elements, respectively.

Example

There are a few useful variants:

One are the *Last variants, which do the same thing, but for the last n elements.

fun <T> List<T>.takeLast(n: Int): List<T>
fun <T> List<T>.dropLast(n: Int): List<T>

Example

Another are the *While variants, which do the same thing, but instead of taking/dropping a fixed amount of elements, they accept a predicate and take/drop elements while that predicate is true:

inline fun <T> Iterable<T>.takeWhile(
    predicate: (T) -> Boolean
): List<T>
inline fun <T> Iterable<T>.dropWhile(
    predicate: (T) -> Boolean
): List<T>

Example

You also have *LastWhile variants, which are the combination of the previous two.

inline fun <T> Iterable<T>.takeLastWhile(
    predicate: (T) -> Boolean
): List<T>
inline fun <T> Iterable<T>.dropLastWhile(
    predicate: (T) -> Boolean
): List<T>

slice

fun <T> List<T>.slice(indices: IntRange): List<T>
fun <T> List<T>.slice(indices: Iterable<Int>): List<T>

The slice function comes in two variants — one returns all the elements in a certain index range, while the other returns the elements at the specified indices. Both could be implemented in terms of filterIndexed.

Example

Miscellaneous

distinct, distinctBy

fun <T> Iterable<T>.distinct(): List<T>
inline fun <T, K> Iterable<T>.distinctBy(
    selector: (T) -> K
): List<T>

Returns a List with all duplicate elements removed. The distinctBy variant removes all elements for which selector returns the same value.

Example

Go back to Collection Operations: Transformations, jump to the Table of Contents, or continue to Collection Operations: Single Element Access.

Java
Kotlin
Programming
Collection
Functional Programming
Recommended from ReadMedium