avatarGabriel Shanahan

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

3140

Abstract

&url=https%3A%2F%2Fpl.kotl.in%2FbnaqJDXvp&key=a19fcc184b9711e1b4764040d3dc5c07&type=text%2Fhtml&schema=kotl" allowfullscreen="" frameborder="0" height="300" width="800"> </div> </div> </figure></iframe></div></div></figure><p id="75f4">There are also variations of <code>any</code> and <code>none</code> that take no arguments, and return <code>true</code> if the collection has at least one element and no elements, respectively.</p><p id="2835"><b>Example</b></p> <figure id="4b66"> <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%2FMrBtACGD0&amp;display_name=Kotlin+Playground&amp;url=https%3A%2F%2Fpl.kotl.in%2FMrBtACGD0&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="7171">isEmpty, isNotEmpty, ifEmpty</h2><div id="19be"><pre><span class="hljs-function"><span class="hljs-keyword">fun</span> <span class="hljs-title">isEmpty</span><span class="hljs-params">()</span></span>: <span class="hljs-built_in">Boolean</span></pre></div><div id="c46c"><pre><span class="hljs-keyword">inline</span> <span class="hljs-function"><span class="hljs-keyword">fun</span> <span class="hljs-type"><T></span> Collection<span class="hljs-type"><T></span>.<span class="hljs-title">isNotEmpty</span><span class="hljs-params">()</span></span>: <span class="hljs-built_in">Boolean</span></pre></div><div id="71c4"><pre><span class="hljs-keyword">inline</span> <span class="hljs-function"><span class="hljs-keyword">fun</span> <span class="hljs-type"><C : Collection<*></span>, R> C.<span class="hljs-title">ifEmpty</span><span class="hljs-params">( defaultValue: () -> <span class="hljs-type">R</span> )</span></span>: R <span class="hljs-keyword">where</span> C : R</pre></div><p id="a695">The <code>isEmpty</code> method is not an extension, but defined directly on the <code>Collection</code> interface. Predictably, it returns <code>true</code> if the collection contains no elements. For <code>List</code>, <code>none</code> is an alias for <code>isEmpty</code>. The opposite of <code>isEmpty</code> is <code>isNotEmpty</code>. There is also <code>isNullOrEmpty</code>, which is defined on a nullable receiver:</p><div id="e72c"><pre><span class="hljs-keyword">inline</span> <span class="hljs-function"><span class="hljs-keyword">fun</span> <span class="hljs-type"><T></span> Collection<span class="hljs-type"><T></span>?.<span class="hljs-title">isNullOrEmpty</span><span class="hljs-params">()</span></span>: <span class="hljs-built_in">Boolean</span></pre></div><p id="f307">Of interest is the <code>ifEmpty</code> method, which allows us to substitute a different <code>Collection</code> if the receiver is empty. Notice that the type of the return value is always the type of what is returned

Options

by <code>defaultValue</code>, which can be a super-type of the receiver.</p><p id="ac65"><b>Example</b></p> <figure id="308d"> <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%2FFKL76YQlm&amp;display_name=Kotlin+Playground&amp;url=https%3A%2F%2Fpl.kotl.in%2FFKL76YQlm&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="063f">contains, containsAll</h2><div id="ebda"><pre><span class="hljs-keyword">operator</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">contains</span><span class="hljs-params">(element: <span class="hljs-type">T</span>)</span></span>: <span class="hljs-built_in">Boolean</span></pre></div><div id="5e72"><pre><span class="hljs-keyword">inline</span> <span class="hljs-function"><span class="hljs-keyword">fun</span> <span class="hljs-type"><T></span> Collection<span class="hljs-type"><T></span>.<span class="hljs-title">containsAll</span><span class="hljs-params">( elements: <span class="hljs-type">Collection</span><<span class="hljs-type">T</span>> )</span></span>: <span class="hljs-built_in">Boolean</span></pre></div><p id="d7c5">We already know <code>contains</code> from the chapter on <a href="https://readmedium.com/operators-6625ca0b8a63">operators</a>. It can be invoked using the <code>in</code> keyword. The <code>containsAll</code> expands on this functionality by returning <code>true</code> only if the receiver contains all the elements of the argument, and <code>false</code> otherwise. Naturally, it is not an operator.</p><p id="72d3"><b>Example</b></p> <figure id="f4ee"> <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%2F_j_C24WuU&amp;display_name=Kotlin+Playground&amp;url=https%3A%2F%2Fpl.kotl.in%2F_j_C24WuU&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="4a4c">Go back to <a href="https://readmedium.com/collection-operations-single-element-access-ebdea8fe451c">Collection Operations: Single Element Access</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-aggregators-304aa23cceed">Collection Operations: Aggregators</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: Predicates

An introduction to the most important functions for predicates: all, any, none, isEmpty, isNotEmpty, ifEmpty, contains, containsAll, 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.

all, any, none

inline fun <T> Iterable<T>.all(predicate: (T) -> Boolean): Boolean
inline fun <T> Iterable<T>.any(predicate: (T) -> Boolean): Boolean

The all method accepts a predicate and returns true if every element satisfies the predicate, and false otherwise. The some method has the same input, but returns true if at least one element satisfies the predicate, and false otherwise.

Example

There are also variations of any and none that take no arguments, and return true if the collection has at least one element and no elements, respectively.

Example

isEmpty, isNotEmpty, ifEmpty

fun isEmpty(): Boolean
inline fun <T> Collection<T>.isNotEmpty(): Boolean
inline fun <C : Collection<*>, R> C.ifEmpty(
    defaultValue: () -> R
): R where C : R

The isEmpty method is not an extension, but defined directly on the Collection interface. Predictably, it returns true if the collection contains no elements. For List, none is an alias for isEmpty. The opposite of isEmpty is isNotEmpty. There is also isNullOrEmpty, which is defined on a nullable receiver:

inline fun <T> Collection<T>?.isNullOrEmpty(): Boolean

Of interest is the ifEmpty method, which allows us to substitute a different Collection if the receiver is empty. Notice that the type of the return value is always the type of what is returned by defaultValue, which can be a super-type of the receiver.

Example

contains, containsAll

operator fun <T> Iterable<T>.contains(element: T): Boolean
inline fun <T> Collection<T>.containsAll(
    elements: Collection<T>
): Boolean

We already know contains from the chapter on operators. It can be invoked using the in keyword. The containsAll expands on this functionality by returning true only if the receiver contains all the elements of the argument, and false otherwise. Naturally, it is not an operator.

Example

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

Java
Kotlin
Programming
Collection
Functional Programming
Recommended from ReadMedium