Home » JVM Languages » Kotlin

Kotlin

Deriving a Kotlin “Try” type

Functional programming languages like Scala often have a type called “Try” to hold the result of a computation if successful or to capture an exception on failure.  This is an incredibly useful type, allowing a caller to pointedly control how to handle an exceptional scenario. In this post I will try and create such a type from scratch. As an example, ...

Read More »

Kotlin dependency injection with Koin

Dependency injection is a common technique in today’s software design. With dependency injection we pass dependencies to a component instead of creating it inside the component. This way we can separate construction and use of dependencies. In this post we will look at Koin, a lightweight Kotlin dependency injection library. Koin describes itself as a DSL, a light container and ...

Read More »

Jackson Kotlin extension and reified types

Jackson Kotlin module library is a pleasure to use. It greatly simplifies gnarly code, specifically one’s involvingTypeReference.  Consider a sample json which looks like this: 1 2 3 4 5 {     "a" : ["b", "c"],     "b" : ["a", "c"],     "c" : ["a", "b"] } This content can be represented as a “Map<List<String>>” type in Java.  So now, if I were ...

Read More »

Top “n” using a Priority Queue

If you ever need to capture the smallest or largest “n” from a stream of data, the approach more often than not will be to use a simple data-structure called the Priority Queue.  Priority Queues do one thing very well – once a bunch of data is added, it can return the lowest value (or the highest value) in constant ...

Read More »

Kotlin – Convert List to Map Examples

1. Overview In this tutorial, We’ll learn how to convert the List to Map in Kotlin programming. Let us explore the different ways to do it. 1. Overview In this tutorial, We’ll learn how to convert the List to Map in Kotlin programming. Let us explore the different ways to do this conversion. Kotlin provides a set of methods for ...

Read More »

How To Convert String to Float in Kotlin?

1. Overview In this tutorial, We will learn how to convert the String value to Float in Kotlin. This conversion is done using toFloat() method of String class. But there are many cases where it gives parsing errors for wrong inputs. 2. Kotlin String to Float using toFloat() Method Converting string to float is done with toFloat() method of string ...

Read More »

Kotlin – Convert Map to List Examples

1. Overview In this tutorial, We will learn how to convert the Map to List in kotlin programming. Map implementations such as HashMap or TreeMap can be converted into an ArrayList or List object. Let us explore thedifferent ways to do this conversion. 2. Converting HashMap to List in Kotlin Using ArrayList Constructor This approach is the simple one and ...

Read More »

Kotlin: Multi variable null check

Although not-null, immutable variables with sensible defaults are often desired, it is not always the case. It is sometimes required to check if multiple variables are not null. This post describes several variants of how to do this when smart casts are not possible because the variables are mutable. Consider the following example: class Person() { var name: String? = ...

Read More »