Kotlin

Kotlin Kandy: Get Text Before Or After A Delimiter

Posted on by  
Hubert Klein Ikkink

Kotlin adds the substringBefore, substringBeforeLast, substringAfter and substringAfterLast extension functions to the String class. Instead of using indices to get a substring you can use a string or character value. The functions without Last use the first occurrence of the delimiter and the methods with Last use the last occurrence. If the delimiter is not found the original string is returned. You can supply a value that should be returned when the delimiter is not found.

Continue reading →

Kotlin Kandy: Get A Random Element From A Collection

Posted on by  
Hubert Klein Ikkink

Kotlin has very useful extensions functions for working with collections. These extension functions make working with collections more easy and fun. One of the extension functions is the random function. When you call random() Kotlin returns a single element from the collection using the default random source. The function also accepts a Random instance as argument. This instance has a seeded value to return repeatable random values.

Continue reading →

Kotlin Kandy: Transform Map Keys Or Values With mapKeys And mapValues

Posted on by  
Hubert Klein Ikkink

Sometimes you want to transform only the keys in a Map, or only transform the values. Kotlin has two useful methods to achieve this: mapKeys and mapValues. You can use mapKeys to transform the keys of the map while keeping the values the same. With mapValues you can transform the values of the map while keeping the keys the same. Both methods accept a lambda function as asrgument of type Map.Entry and must return the new key or value. In order to transform the keys or values and add the result to an existing Map you can use the methods mapKeysTo and mapValuesTo. The first argument is the existing (mutable) Map and the second argument is the lambda function.

Continue reading →

Recap: Kotlin Dev Day 2025

Posted on by  
Jacob van Lingen

The day before yesterday, I had the opportunity to attend Kotlin Dev Day. The event featured five parallel lanes, so my experience reflects just one perspective, yours could be completely different. Join me as I share the highlights of my day!

Continue reading →

Kotlin Kandy: Create Fix Sized Sublists From Iterables With chunked

Posted on by  
Hubert Klein Ikkink

The chunked extension method is added to the Iterable Java class and makes it possible to split an interable into fixed sized lists. We define the size of the lists as argument to the chunked method. The return result is a list of lists. Each of the lists will have the number of elements we have specified as argument. The last list can have less elements if the total number of elements cannot be divided exactly by the size we specified as argument. We can specify a lambda transformation function as second argument. The lambda function has the new sublist as argument and we can write code to transform that sublist.

Continue reading →

Kotlin Kandy: Split Collection Or String With Partition

Posted on by  
Hubert Klein Ikkink

The method partition is available in Kotlin for arrays and iterable objects to split it into two lists. We pass a predicate lambda function to the partition method. The predicate should return either true or false based on a condition for each element from the array or iterable. The return result is a Pair instance where the first element is a List object with all elements that returned true from the predicate. The second element in the Pair object contains all elements for which the predicate returned false. As a String can be seen as an iterable of characters we can also use partition on a String instance.

Continue reading →

Kotlin Kandy: Taking Or Dropping Characters From A String

Posted on by  
Hubert Klein Ikkink

Kotlin adds a lot of extension methods to the String class. For example we can use the take method to get a certain number of characters from the start of a string value. With the drop method where we remove a given number of characters from the start of the string to get a new string. We can also take and drop a certain number of characters from the end of a string using the methods takeLast and dropLast.

Instead of using the number of characters we want to take or drop we can also use a condition defined with a predicate lambda function. We take or drop characters as long as the lambda returns true. The names of the methods for taking characters are takeWhile and takeLastWhile and for dropping characters dropWhile and dropLastWhile.

Continue reading →

Kotlin Kandy: Find Common Prefix Or Suffix In Strings

Posted on by  
Hubert Klein Ikkink

If we want to find the longest shared prefix or suffix for two string values we can use the String extension methods commonPrefixWith and commonSuffixWith. The result is the prefix or suffix value that is common for both values. We can pass a second argument to the method to indicate if we want to ignore the casing of the letters. The default value for this argument is false, so if we don’t set it explicitly the casing of the letters should also match.

Continue reading →

shadow-left