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&display_name=Kotlin+Playground&url=https%3A%2F%2Fpl.kotl.in%2FMrBtACGD0&key=a19fcc184b9711e1b4764040d3dc5c07&type=text%2Fhtml&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&display_name=Kotlin+Playground&url=https%3A%2F%2Fpl.kotl.in%2FFKL76YQlm&key=a19fcc184b9711e1b4764040d3dc5c07&type=text%2Fhtml&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&display_name=Kotlin+Playground&url=https%3A%2F%2Fpl.kotl.in%2F_j_C24WuU&key=a19fcc184b9711e1b4764040d3dc5c07&type=text%2Fhtml&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>