Updated April 10, 2023
Introduction to Kotlin Filter
The kotlin filter() is one of the default methods and it is used to prominent the task for processing the collection datas in the list and it will return the element which contains all the elements is associated with the list it has to be matched with the mentioned predicate values it may be of any types like boolean condition for the functional interface the value is stored and retrieved using the index-based concept the index position is to be passed as the two different arguments it also supports both positive and negative value conditions.
Syntax
In kotlin language, the supertype for all the types of the object is the root and parent class it is more similar to the java language. Like that filter() is one of the kotlin pre-defined methods it can be filtered with the normal predicate and it returns with some matched collection elements.
fun main()
{
val vars1 : collections like map()
val vars2=vars1.filter{ --- some input conditions depends upon the requirement---}
---some kotlin logics depends upon the requirement---
}
The above codes are the basic syntax for utilizing the filter() method on the default packages like collections and other util packages.
How does the filter function work in Kotlin?
The kotlin filter function is used to create the separate list as a new list based upon the predicated data. While we filtering the user data the results which called on by using the function that allows us to accumulate the outputs of the filter’s datas with different arrays. So that we have taken the help of filterTo() method for the mutable list copy datas for the mutable arrays which is already declared and initialized to the memory. When we use the filter option it allows us to take several collections and combined to form the filter with the single and accumulative collection data.
While filtering data the function also allows us to accumulate the output results of the filters with different arrays. It filters them into the single and accumulative collection data if the array contains the null elements the beneficial pair of the filter methods is in two methods like filterNotNull and filterNotNullTo which just want to filter out the null elements in the list stack. The predicated also follows up the lambda expressions that can be combined with both the elements and its index position. Because the filter function represents the predicate that goes to be the boolean-valued format for the parameters.
Examples of Kotlin Filter
Examples of Kotlin Filter are given below:
Example #1
Code:
package one;
fun main(args: Array<String>)
{
val news = listOf("Welcome","To","My","Domain","Have", "a", "nice", "day users please try and use our application")
val eg=listOf("Today is Sunday","Today is Monday", "Today is Tuesday", "Today is Wednesday", "Today is Thursday", "Today is Friday", "Today is Saturday")
val news1 = news.filter { it.length > 7 }
println("Thank you users your input string length is $news1")
val news2 = mapOf("key1" to 11, "key2" to 21,
"key3" to 31, "key4" to 41, "key5" to 51, "key6" to 61, "key7" to 71, "key8" to 81, "key9" to 91, "key10" to 101 )
val news3 = news2.filter { (key, value) ->
key.endsWith("7") && value > 19}
println("Thank you users after filtering your input string the result is $news3")
val news4= eg.filter{ it.length > 9}
println(news4)
val news5 = mapOf("key11" to 12, "key12" to 22,
"key13" to 32, "key14" to 42, "key15" to 52, "key16" to 62, "key17" to 72, "key18" to 82, "key19" to 92, "key20" to 102 )
val news6= news5.filter {(key,value) ->key.endsWith("9") && value > 29}
println(news5)
}
Output:
In the above example, we can filter the string values in the list and print it on the console.
Example #2
Code:
package one;
data class first(val ex1: String, val ex2: Int, val ex3: Int)
fun demo(eg : List<first>,vars3: Int) : first?{
if(vars3 == 2) return null
return eg[vars3]
}
fun main(args: Array<String>)
{
println("Welcome To My Domain have a nice day users please keep on spenting time with our application")
val eg1 = listOf(11,22,33,44,55,66,77,88,99,54, 12,22,33)
val eg2 = listOf(44,66,88,10,12)
val eg3 = eg1.filter { eg2.contains(it) }
print(eg3)
val results =
listOf(
first("Thank you users this is January month", 31, 0),
first("Thank you users this is February month", 28, 1),
first("Thank you users this is March month", 31, 2),
first("Thank you users this is April month", 30, 3),
first("Thank you users this is May month", 31, 4),
first("Thank you users this is June month", 30, 5),
first("Thank you users this is July month",31, 6),
first("Thank you users this is August month",30, 7),
first("Thank you users this is September month",31, 8),
first("Thank you users this is October month",31, 9),
first("Thank you users this is November month",30, 10),
first("Thank you users this is December month",31, 11)
)
val demo1 = results.indexOfFirst { it.ex1 == "Thank you users this is April month" && it.ex2 == 30 }
val demo2 = results.indexOfFirst { it.ex1 == "Thank you users this is May month" && it.ex2 == 31 }
val demo3 = results.indexOfFirst { it.ex1 == "Thank you users this is August month" && it.ex2 == 31 }
println(demo(results,demo1))
println(demo(results,demo2))
println(demo(results,demo3))
println("Thank you users for spending the time with our application")
}
Output:
In the second example, we can filter the common values in the two lists that we can filter the month’s total days using the index-based condition and print it on the console.
Example #3
Code:
package one;
fun main(args: Array<String>)
{
println("Welcome To My Domain have a nice day users please keep on spending time with our application")
val str1 = "Have a Nice Day users"
val result = str1.filter({ it -> it.isUpperCase() })
println("Thank you users your Filtered String is : " + result)
val str2 = "Please try again"
val result2 = str2.filter({ it -> it.isLetter() })
println("Thank you users your Filtered String is : " + result2)
println("Thank you users for spending the time with our application")
val str3 = "Thank you for 432spenting the time"
val result3 = str3.filter({ it -> it.isDigit() })
println("Thank you users your Filtered String is : " + result3)
}
Output:
In the final example, we can filter and calculate the upper characters in the string variable using the filter() method.
Conclusion
In conclusion, part kotlin filter is one of the collection methods and its values are iterated and validate the conditions using the statements. Here we can sort out the user input conditions based on that the values are fetched out on the memory list. Before that it checks the filter condition and then it validates and processed it on the backend nodes.
Recommended Articles
This is a guide to Kotlin filter. Here we discuss the introduction, syntax, and working of filters in Kotlin along with different examples and its code implementation. You may also have a look at the following articles to learn more –