{"id":3093,"date":"2020-01-27T14:21:34","date_gmt":"2020-01-27T19:21:34","guid":{"rendered":"http:\/\/bluegalaxy.info\/codewalk\/?p=3093"},"modified":"2020-01-27T15:40:09","modified_gmt":"2020-01-27T20:40:09","slug":"kotlin-how-to-use-filter-it-and-operator","status":"publish","type":"post","link":"https:\/\/bluegalaxy.info\/codewalk\/2020\/01\/27\/kotlin-how-to-use-filter-it-and-operator\/","title":{"rendered":"Kotlin: How to use .filter( ), &#8216;it&#8217; key word, and arrow &#8216;->&#8217; operator"},"content":{"rendered":"\n<p>Kotlin&#8217;s built-in <code>.filter()<\/code> function is meant to iterate through any iterable collection type, (lists, sets, or maps) and check each element of the collection against some criteria (aka &#8220;predicate&#8221;). If the element matches the predicate, meaning the boolean result is <code>true<\/code>, then the element is added to a result collection of the same type that is being checked. <br><br>For example, if checking a list of strings for any string that is not empty, every string that is not empty will be placed in a result list. <br><br>If checking a map of names and phone numbers for valid phone numbers, where names are keys and phone numbers are values, then every valid name \/ phone number combination will be added to a result map. <\/p>\n\n\n\n<p>Here is what kotlinlang.org says about <code>filter()<\/code>:<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\"><p>In Kotlin, filtering conditions are defined by&nbsp;<em>predicates<\/em>&nbsp;\u2013 lambda functions that take a collection element and return a boolean value:&nbsp;<code>true<\/code>&nbsp;means that the given element matches the predicate,&nbsp;<code>false<\/code>&nbsp;means the opposite.<\/p><p>The basic filtering function is <a href=\"https:\/\/kotlinlang.org\/api\/latest\/jvm\/stdlib\/kotlin.collections\/filter.html\">filter()<\/a>. When called with a predicate,&nbsp;<code>filter()<\/code>&nbsp;returns the collection elements that match it. For both&nbsp;<code>List<\/code>&nbsp;and&nbsp;<code>Set<\/code>, the resulting collection is a&nbsp;<code>List<\/code>, for&nbsp;<code>Map<\/code>&nbsp;it&#8217;s a&nbsp;<code>Map<\/code>&nbsp;as well.<\/p><\/blockquote>\n\n\n\n<p>The syntax for using <code>.filter<\/code> on a collection is:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"no-highlight\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">collection.filter { predicate }<\/pre>\n\n\n\n<p>Where &#8220;predictate&#8221; is a boolean check that is run on each item in the collection.<br><strong><br>Note:<\/strong> What is odd about this syntax is that where we would expect to see parenthesis used, as in <code>.filter()<\/code>, curly braces are used instead. i.e. <code>.filter { }<\/code>. This is because Kotlin allows the parenthesis to be left off when defining as function in line. The parenthesis indicate the beginning and end of the lambda function definition. This syntax also works: <code>.filter() { }<\/code><\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Example 1: Using .filter on a list (or set) collection<\/h3>\n\n\n\n<p>Here is an example of running filter on a list (or set):<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"kotlin\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">val numbers = listOf(\"one\", \"two\", \"three\", \"four\")  \nval longerThan3 = numbers.filter { it.length > 3 }\nprintln(longerThan3)\n\n\/\/ or\n\nval numberOne = numbers.filter { it == \"one\" }\nprintln(numberOne)<\/pre>\n\n\n\n<p>Which yields: <\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"no-highlight\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">[three, four]\nor\n[one]<\/pre>\n\n\n\n<p>Here the collection is a list of strings called &#8220;numbers&#8221;. <br>The predicate is a check of the length of each string. <\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"no-highlight\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">numbers.filter { it.length > 3 }<\/pre>\n\n\n\n<p>The <code>it<\/code> keyword is the name that Kotlin gives to the next single parameter in a collection as it is iterated on by a lambda function such as .filter.<br><br>The output is another list called &#8220;longerThan3&#8221;, which will contain a list of all of the elements of the collection that passed the predicate check with a boolean value of <code>true<\/code>.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Example 2: Using .filter on a map collection<\/h3>\n\n\n\n<p>Here is an example of running filter on a map, which is a collection of key &#8211; value pairs. <\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"kotlin\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">val numbersMap = mapOf(\"key1\" to 1, \"key2\" to 2, \"key3\" to 3, \"key11\" to 11)\nval filteredMap = numbersMap.filter { (key, value) -> key.endsWith(\"1\") &amp;&amp; value > 10}\nprintln(filteredMap)<\/pre>\n\n\n\n<p>Since a map is a collection of key \/ value pairs we use <code>(key, value)<\/code> in place of <code>it<\/code> on the parameter side, we use arrow <code>-&gt;<\/code> notation on the left side of the predicate.  For example:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"no-highlight\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">.filter { (key, value) -><\/pre>\n\n\n\n<p>And since we have key and value in map collections, we can use either key or value or both in the predicate. For example:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"no-highlight\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">key.endsWith(\"1\") &amp;&amp; value > 10}<\/pre>\n\n\n\n<p>The above example of using .filter on a map collection yields:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"no-highlight\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">{key11=11}<\/pre>\n\n\n\n<p>Here is what the Kotlin documentation says about the arrow <code>-&gt;<\/code> operator:<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\"><p>separates the parameters and body of a&nbsp;<a href=\"https:\/\/kotlinlang.org\/docs\/reference\/lambdas.html#lambda-expression-syntax\">lambda expression<\/a><\/p><p>separates the parameters and return type declaration in a&nbsp;<a href=\"https:\/\/kotlinlang.org\/docs\/reference\/lambdas.html#function-types\">function type<\/a><\/p><p>separates the condition and body of a&nbsp;<a href=\"http:\/\/kotlinlang.org\/docs\/reference\/control-flow.html#when-expression\">when expression<\/a>&nbsp;branch<\/p><\/blockquote>\n\n\n\n<p>For more details about <code>.filter()<\/code>, see:<br><a href=\"https:\/\/kotlinlang.org\/docs\/reference\/collection-filtering.html#filtering\">https:\/\/kotlinlang.org\/docs\/reference\/collection-filtering.html#filtering<\/a><br><br>For more information about the <code>it<\/code> key word, go <a href=\"https:\/\/kotlinlang.org\/docs\/reference\/lambdas.html?_ga=2.243533637.1329100594.1579621406-1336266263.1574118842#it-implicit-name-of-a-single-parameter\">here<\/a>.<br><br>For more information about the <code>-&gt;<\/code> operator, see:<br><a href=\"https:\/\/kotlinlang.org\/docs\/reference\/keyword-reference.html\">https:\/\/kotlinlang.org\/docs\/reference\/keyword-reference.html<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Kotlin&#8217;s built-in .filter() function is meant to iterate through any iterable collection type, (lists, sets, or maps) and check each element of the collection against some criteria (aka &#8220;predicate&#8221;). If the element matches the predicate, meaning the boolean result is true, then the element is added to a result collection of the same type that &hellip; <a href=\"https:\/\/bluegalaxy.info\/codewalk\/2020\/01\/27\/kotlin-how-to-use-filter-it-and-operator\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Kotlin: How to use .filter( ), &#8216;it&#8217; key word, and arrow &#8216;->&#8217; operator<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[214],"tags":[215],"class_list":["post-3093","post","type-post","status-publish","format-standard","hentry","category-kotlin","tag-kotlin"],"_links":{"self":[{"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/posts\/3093","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/comments?post=3093"}],"version-history":[{"count":18,"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/posts\/3093\/revisions"}],"predecessor-version":[{"id":3113,"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/posts\/3093\/revisions\/3113"}],"wp:attachment":[{"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/media?parent=3093"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/categories?post=3093"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/tags?post=3093"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}