Sunday, 19 December 2021

Passing function as a param to collection filter

It's actually simpler to provide a lambda directly, There's other option that's worth covering, too, which is a function / lambda reference.  If you already had a function that would do the job, you don't need to put it in a lambda, but can refer to it directly, through function or its reference using the :: notation. this will works only if the parameter type(s) are compatible, but it's slightly simpler and can generate slightly more efficient bytecode.

val myValues = listOf(12, 25, 215, 3, 52)
println("All Values: $myValues")
println("Even Filter ${myValues.filter { it % 2 == 0 }} -- Simple implicit lambda")
println("Odd Filter ${myValues.filter { oddFilter(it) }} -- lambda reference as param")
println("odd Filter ${myValues.filter(oddFilter)} -- lambda as param to filter function")
println("Even Filter ${myValues.filter(::evenFilter)} -- function reference as param")

val oddFilter = { a: Int -> a % 2 != 0 }

fun evenFilter(g: Int): Boolean = g % 2 == 0


All Values: [12, 25, 215, 3, 52]
Even Filter [12, 52] -- Simple implicit lambda
Odd Filter [25, 215, 3] -- lambda reference as param
odd Filter [25, 215, 3] -- lambda as param to filter function
Even Filter [12, 52] -- function reference as param

Happy Coding :-)

Saturday, 13 November 2021

Vetoable - Delegates -- Kotlin

Vetoable, this allows us to modify the values when the argument input by the user(s) fulfills the specified condition, it can be used in place of observable properties if the user wants to intercept assignment. Vetoable is like Observable Properties with additional features to allows to modify and notify the values when the condition is met.

Simple example: 

The string which you never want to be reassigned with empty or to some specific text. 

The number values which is assigned should be match some condition.  

The generic observable kind of where the condition check will happen and then the value will be get assigned to the variable, incase if the condition fails, older value will be retained. 

var acceptOnlyEven: Int by Delegates.vetoable(0) { property, prevStr, nextStr ->
        nextStr.mod(2) == 0

var allowNonEmptyText: String by Delegates.vetoable("NA") { property, oldValue, newValue ->

var acceptStatus: String by Delegates.vetoable(STATUS_START) { property, oldValue, newValue ->
        listOf(STATUS_START, STATUS_INPROGRESS, STATUS_DONE).contains(newValue)

println("Str: $acceptStatus")
acceptStatus = "Status"
println("Str: $acceptStatus")
acceptStatus = ""
println("Str: $acceptStatus")
acceptStatus = "DonE"
println("Str: $acceptStatus")
acceptStatus = "Done"
println("Str: $acceptStatus")

println("Value: $acceptOnlyEven")
acceptOnlyEven = 10
println("Value: $acceptOnlyEven")
acceptOnlyEven = 5
println("Value: $acceptOnlyEven")

println("Str: $allowNonEmptyText")
allowNonEmptyText = "Status"
println("Str: $allowNonEmptyText")
allowNonEmptyText = ""
println("Str: $allowNonEmptyText")


Str: Start
Str: Start
Str: Start
Str: Start
Str: Done
Value: 0
Value: 10
Value: 10
Str: NA
Str: Status
Str: Status

Good Luck, Happy Coding :-)

Sunday, 17 October 2021

Kotlin getOrElse & getOrPut


This provides safe access to elements of a collection. It takes an index and a function that provides the default value in cases when the index is out of bound.


This returns the value of the key.  If the key is not found in the map, calls the default value function, puts its result into the map under the given key and returns value of the same.

Code Snippet:

    data class Header(val heading: String, val subHeading: String)    

    val items = listOf("AAA", "BBB", "CCC", "DDD")
    val itemMap = mutableMapOf("x" to "AAA", "y" to "BBB")

    val header = listOf(
        Header("Heading1", "SubHeading1"), Header("Heading2", "SubHeading2")

    println("Item at index 2 ${items.getOrElse(2) { "NA" }}")
    println("Item at index 4 ${items.getOrElse(4) { "NA" }}")
    println("Header at index 1 ${header.getOrElse(1) { "NA" }}")
    println("Header at index 2 ${header.getOrElse(2) { "NA" }}")
    println("Map Data: $itemMap")
    println("Map Key x ${itemMap.getOrElse("x") { "NA" }}")
    println("Map Key z ${itemMap.getOrElse("z") { "NA" }}")
    println("Map Key x ${itemMap.getOrPut("x") { "XXX" }}")
    println("Map Key z ${itemMap.getOrPut("z") { "ZZZ" }}")

    // Remove Map Key "x"

    println("Refreshed Map Data: $itemMap")
    println("Map Key x ${itemMap.getOrPut("x") { "AAA" }}")
    println("Refreshed Map Data: $itemMap")


Item at index 2 CCC
Item at index 4 NA
Header at index 1 Header(heading=Heading2, subHeading=SubHeading2)
Header at index 2 NA
Map Data: {x=AAA, y=BBB}
Map Key x AAA
Map Key z NA
Map Key x AAA
Map Key z ZZZ
Refreshed Map Data: {y=BBB, z=ZZZ}
Map Key x AAA
Refreshed Map Data: {y=BBB, z=ZZZ, x=AAA}

Happy Coding :-)

Sunday, 26 September 2021

groupBy vs associateBy in Kotlin

associateBy and groupBy function helps to build the maps from the elements of a collection indexed by the specified key. 

keySelector: The key is defined in the keySelector parameter. 

valueSelector: We can also specify an optional valueSelector to define what will be stored in the value of the map element, in case of not specifying the same it will consider the complete object as a value. 

The difference between associateBy and groupBy is about how they process the objects with the respective key

associateBy - It uses the last suitable element as the result value.

groupBy - it produces the list of all suitable elements and puts it in the result value(s).

P.S. The returned map preserves the entry iteration order of the original collection.

val boardingList = listOf(
        OnBoarding("1", "AAA", "Chennai", "Bang", "IT"),
        OnBoarding("2", "BBB", "Bang", "Hyd", "IT"),
        OnBoarding("3", "CCC", "Bang", "Chennai", "Finance"),
        OnBoarding("4", "DDD", "Hyd", "Pune", "Finance"),
        OnBoarding("5", "DDD", "Chennai", "Bang", "IT")

// Param either be 'it' or field references, both does the same job.
println(boardingList.groupBy({ it.baseLocation }, { it.eName })) 
println(boardingList.groupBy(OnBoarding::baseLocation, OnBoarding::eName))

println(boardingList.associateBy({ it.baseLocation }, { it.eName }))


{Chennai=[AAA, DDD], Bang=[BBB, CCC], Hyd=[DDD]}
{Chennai=[AAA, DDD], Bang=[BBB, CCC], Hyd=[DDD]}
{Chennai=DDD, Bang=CCC, Hyd=DDD}
{Chennai=DDD, Bang=CCC, Hyd=DDD}

Good Luck, Happy Coding :-)

Sunday, 12 September 2021

DSL Code References

We should use DSL to simplify and improve the complexity of the app and make the code more readable, Before jumping into writing our own DSL we need to aware about the concept of lambda with receiver, invoke and operator.

Assume an example we have a class called Device, Please see below different ways how the object got initialized

class Device() {
    var id: Int = 0
    var name: String = ""
    var make: String = ""

    fun printDeviceInfo() {
        println("ID:  $id | Name: $name | Make: $make")

    operator fun invoke(device: Device.() -> Unit): Device {
        return this

Now, to instantiate the Class we can create an object like below ways,

// Traditional Way
    val env = Device() = 102 = "5.4 GHz Wifi"
    env.make = "Amazon-Cable"

// Invoke Way
    val en = Device()
    val obj = en {
        id = 102
        name = "5.4 GHz Wifi"
        make = "Amazon-Cable"

There is also other way where we are able to do without using invoke operator function, but here we can't use the traditional way of object creation. Since we pass it as parameter to the class primary constructor and followed by init method should call the same.

class Device(device: Device.() -> Unit) {
    var id: Int = 0
    var name: String = ""
    var make: String = ""

    fun printDeviceInfo() {
        println("ID:  $id | Name: $name | Make: $make")

    init {

val device = Device {
        id = 102
        name = "5.4 GHz Wifi"
        make = "Amazon-Cable"

Here we can also use it on the Data class also, but we should declare the constructor param as val/var, whereas it is not required on the normal model class like above.

data class User(val obj: User.() -> Unit) {
    var id: String = "NA"
    var age: Long = 0

    init {

fun main() {
    val userObj = User {}
    val usersList = listOf(userObj, User {
        id = "ABC567"
        age = 28
    }, User {
        id = "BCG678"
        age = 24
    }, User {

    usersList[0].id = "BGH905"

fun printUser(user: User) {
    println("Id: ${}|| Age: ${user.age.takeIf { it > 0 } ?: "NA"}")

Good Luck :-) Happy Coding !!!

Sunday, 22 August 2021

Deduplicating Collection Items

Deduplication refers to a method of eliminating a dataset's redundant data, here in Kotlin we normally use the toSet() function to get the unique datasets, now the distinct() and distinctBy{} joins the club. Please refer the below code snippet for more details, the below example uses both primitive typed object and data class object (Be careful on the data order, this will give an impact).

data class Tez(val id: String)

val users = listOf("AAA", "aaa", "BBB", "bbb", "CCC", "ccc", "AAA", "aaa", "BBB", "bbb", "CCC", "ccc")
    val tezUsers = listOf(
    println("toSet: ${users.toSet()}")
    println("Data Object toSet: ${tezUsers.toSet()}")
    println("distinct : ${users.distinct()}")
    println("Data object distinct: ${tezUsers.distinct()}")
    println("distinctBy: ${users.distinctBy { it.lowercase() }}")
    println("Data object distinctBy: ${tezUsers.distinctBy { }}")

    val revUsers = users.reversed()
    val revTezUsers = tezUsers.reversed()
    println("\n\nreversed toSet: ${revUsers.toSet()}")
    println("reversed Data object toSet: ${revTezUsers.toSet()}")
    println("reversed Distinct : ${revUsers.distinct()}")
    println("reversed Data object distinct: ${revTezUsers.distinct()}")
    println("reversed object distinctBy: ${revUsers.distinctBy { it.lowercase() }}")
    println("reversed Data object distinctBy: ${revTezUsers.distinctBy { }}")


toSet: [AAA, aaa, BBB, bbb, CCC, ccc]
Data Object toSet: [Tez(id=AAA), Tez(id=aaa), Tez(id=BBB), Tez(id=bbb), Tez(id=CCC), Tez(id=ccc)]
distinct : [AAA, aaa, BBB, bbb, CCC, ccc]
Data object distinct: [Tez(id=AAA), Tez(id=aaa), Tez(id=BBB), Tez(id=bbb), Tez(id=CCC), Tez(id=ccc)]
distinctBy: [AAA, BBB, CCC]
Data object distinctBy: [Tez(id=AAA), Tez(id=BBB), Tez(id=CCC)]

reversed toSet: [ccc, CCC, bbb, BBB, aaa, AAA]
reversed Data object toSet: [Tez(id=ccc), Tez(id=CCC), Tez(id=bbb), Tez(id=BBB), Tez(id=aaa), Tez(id=AAA)]
reversed Distinct : [ccc, CCC, bbb, BBB, aaa, AAA]
reversed Data object distinct: [Tez(id=ccc), Tez(id=CCC), Tez(id=bbb), Tez(id=BBB), Tez(id=aaa), Tez(id=AAA)]
reversed object distinctBy: [ccc, bbb, aaa]
reversed Data object distinctBy: [Tez(id=ccc), Tez(id=bbb), Tez(id=aaa)]

Please refer the above highlighted output, produces the different output due to dataset order.  Also, in the lambda it.lowercase() that won't change the dataset value, it just use for the comparison purpose only.  

Happy Coding :-)

Saturday, 17 July 2021

Capitalize each word of a String in Kotlin

capitalize() the string extension function is got deprecated from Kotlin 1.5 and they have provided an alternative as replaceFirstChar().  Since I'm creating a new extension functon and performing the logic to capitalize the words in the sentence.  We have used Regex() param to the split function, inorder to remove the unnecessary spaces at the end of each words.  In case you are not going to remove those spaces, so you can simply use .split(" ")

fun String.capitalizeWord() = lowercase()
    .joinToString(" ") {

println("thE QuIck    bROwN   FOx".capitalizeWord())
println("thE qUiCk bRown FoX".capitalizeWord())


The Quick Brown Fox
The Quick Brown Fox

Happy Coding :-)

Sunday, 13 June 2021

Kotlin Collection - zip & unzip function

zip function builds one new list of paired elements from two existing arrays. Zipping transformation is useful to combine two array values. The final result is equal to the length of the smaller array if both arrays have different size. The extra elements of the larger array are not included in the final list.

Similarly, to do the reverse transformation, i.e. unzipping, unzip() method is called. In this post, Please refer to the below code snippet for the same. 

In the below example we are associating the states with their corresponding registration code, and in the unzip we are doing reverse the things.   

Here, while doing the zip we are getting the list of pairs, and for the unzip we are using the destructuing it with two separate list of strings. 

Saturday, 12 June 2021

Get Initials for the Display name - String - Android (Kotlin)

This post explains to extract the initial for the display name, i.e. Wanna get the first character of the first two words from the string, below is my code snippet can any one help to optimize it?

For eg: 

Username                         Display initial 

Rajendra prasad guru     RP

Rahul                                 R

Gurunath Desigan            GD

See in the above code snippet, We had tried out the logic in multiple ways, i.e. with Sequence (line 3 to 12) and without sequence (14 to 19).   Here we have added println to see how the internal iteration works, while you are about to use please remove that print statement. 

As per my knowledge all the three are good and optimized in their own way. 

The first one is seems to be optimum, since the split itself has the limit of  2, so it takes only the first 2 split with delimiters. 

This second one adds one more extra function take(2) functions to the logic and it reduces the internal iteration before doing the map transformation function.

The third one is made it as very simple approach, which deals with the split function without sequences and it uses both limit and take function (take function used on the joinToString()'s transforming expression as lambda). 

Sunday, 30 May 2021

Kotlin vs Java - String - Count lambda - Importance

In Kotlin we have count function which exactly provides the same output what we get in the length function.  here we have an overloading function too which takes predicate lambda (Char -> Boolean) as an input param.  

Lets see the below example and hope you will understand.  Here we are about to find the No. of times the character occurs in the string, will see the code snippet in both Kotlin and Java versions.

Kotlin: Type Inference and lambda expression.

val text = "Tester"
val findMe = 'e'
val count = text.count { it == findMe }
println("No. of $findMe in $text is $count")

Output: No. of e in Tester is 2

Java (Version< 8): In an optimized way, here we have written an extended logic by using recursive method and avoids the loops.  

  private static int countOccurences(String someString, char searchedChar, int index) {
        if (index >= someString.length()) {
            return 0;

        int count = someString.charAt(index) == searchedChar ? 1 : 0;
        return count + countOccurences(
                someString, searchedChar, index + 1);

long count = countOccurences("Tester",'e',0);

Output: 2

Java 8: Here the Lambda expression and filter function takes lambda expression and returns the IntStream and from that we can call IntStream functions, here data type has to be explicit and those not type inferences. 

String str="Tester";
char findMe='e';
long count = str.chars().filter(ch -> ch == findMe).count();

Output: 2

Code Snippet: In Kotlin this count function with predicate helps us on many things like password validation helpers  Please refer the below code . 

val password = "T1e st2er$#"
val digits = password.count { it.isDigit() }
val letters = password.count { it.isLetter() }
val lowercase = password.count { it.isLowerCase() }
val letterOrDigit = password.count { it.isLetterOrDigit() }
val uppercase = password.count { it.isUpperCase() }
val whiteSpaces = password.count { it.isWhitespace() }

println("Password: $password  ||| Length: ${password.count()}")
println("# of Digits: $digits")
println("# of Letters: $letters")
println("# of Lowercase char: $lowercase")
println("# of Letter or Digits: $letterOrDigit")
println("# of Uppercase char: $uppercase")
println("# of White Space: $whiteSpaces")


Password: T1e st2er$#  ||| Length: 11
# of Digits: 2
# of Letters: 6
# of Lowercase char: 5
# of Letter or Digits: 8
# of Uppercase char: 1
# of White Space: 1

Sunday, 18 April 2021

Kotlin - Partition Function

Splits the original array / collections into pair of lists, where first list contains elements for which predicate yielded true, while second list contains elements for which predicate yielded false.

Please refer the below examples, here we used 2 filters to get the objects with certain crieteria, but it can be easily achieved through partition function, this executes the predicate and produce the pairs of list and we can access directly on pari's (first and second function) or we can destructure and use the same. 

data class Movies(val id: String,
                      val movieName: String,
                      val rating: Double = 1.0)

    val movies = listOf(Movies("100XA", "Guru", 4.5),
            Movies("100JK", "Ghab", 3.2),
            Movies("100HN", "Qualis", 1.2),
            Movies("1089O", "Tree"))

    // Using Filter
    println("### Rating Above 3 - Filter ###")
    movies.filter { it.rating > 3 }.forEach { println(it.movieName) }

    println("### Rating Not Above 3 - Filter ###")
    movies.filterNot { it.rating > 3 }.forEach { println(it.movieName) }

    // Using Partition - Destructuring the paris of list
    val (above3, notAbove3) = movies.partition { it.rating > 3 }
    println("\n### Rating Above 3 - Partition  ###")
    above3.forEach { println(it.movieName) }

    println("### Rating Not Above 3 - Partition  ###")
    notAbove3.forEach { println(it.movieName) }


### Rating Above 3 - Filter ###
### Rating Not Above 3 - Filter ###

### Rating Above 3 - Partition  ###
### Rating Not Above 3 - Partition  ###

Sunday, 7 March 2021

Kotlin Collection - Mathematic Sets Specific Operation - Union, Intersection & Subract

The Kotlin collections package contains extension functions for popular operations on sets: finding intersections, merging, or subtracting collections from each other.

To merge two collections into one, use the union() function. It can be used in the infix form a union b. Note that for ordered collections the order of the operands is important: in the resulting collection, the elements of the first operand go before the elements of the second.

To find an intersection between two collections (elements present in both of them), use intersect(). To find collection elements not present in another collection, use subtract(). Both these functions can be called in the infix form as well, for example, a intersect b

Code Snippet:

val universityDefCenters = listOf("Delhi", "Mumbai","Puducherry","Chennai")
val universitySouthCenters = listOf("Karnataka", "Puducherry","Chennai", "Kerala")

val allCenters= universityDefCenters union universitySouthCenters
val commonCenters = universityDefCenters intersect universitySouthCenters
val nonSouthCenters = universityDefCenters subtract universitySouthCenters

println("All Centers (Union): $allCenters")
println("Common Centers (Intersection): $commonCenters")
println("Non-South Centers (Subtract): $nonSouthCenters")

All Centers (Union): [Delhi, Mumbai, Puducherry, Chennai, Karnataka, Kerala]
Common Centers (Intersection): [Puducherry, Chennai]
Non-South Centers (Subtract): [Delhi, Mumbai]