Kotlin tutorials

Kotlin example for Beginners – Kotlin Program to Sort ArrayList of Custom Objects By Property

Kotlin Program to Sort ArrayList of Custom Objects By Property In this program, you’ll learn to sort an arraylist of custom object by their given property in Kotlin. Example: Sort ArrayList of Custom Objects By Property import java.util.* fun main(args: Array<String>) { val list = ArrayList<CustomObject>() list.add(CustomObject(“Z”)) list.add(CustomObject(“A”)) list.add(CustomObject(“B”)) list.add(CustomObject(“X”)) list.add(CustomObject(“Aa”)) var sortedList = list.sortedWith(compareBy({ …

Kotlin example for Beginners – Kotlin Program to Sort a Map By Values

Kotlin Program to Sort a Map By Values In this program, you’ll learn to sort a given map by values in Kotlin. Example: Sort a map by values fun main(args: Array<String>) { var capitals = hashMapOf<String, String>() capitals.put(“Nepal”, “Kathmandu”) capitals.put(“India”, “New Delhi”) capitals.put(“United States”, “Washington”) capitals.put(“England”, “London”) capitals.put(“Australia”, “Canberra”) val result = capitals.toList().sortedBy { (_, …

Kotlin example for Beginners – Kotlin Program to Convert InputStream to String

Kotlin Program to Convert InputStream to String In this program, you’ll learn to convert input stream to a string using InputStreamReader in Kotlin. Example: Convert InputStream to String import java.io.* fun main(args: Array<String>) { val stream = ByteArrayInputStream(“Hello there!”.toByteArray()) val sb = StringBuilder() var line: String? val br = BufferedReader(InputStreamReader(stream)) line = br.readLine() while (line …

Kotlin example for Beginners – Kotlin Program to Create String from Contents of a File

Kotlin Program to Create String from Contents of a File In this program, you’ll learn different techniques to create a string from concents of a given file in Kotlin. Before we create a string from a file, we assume we have a file named test.txt in our src folder. Here’s the content of test.txt This is a Test file. Example …

Kotlin example for Beginners – Kotlin Program to Convert Byte Array to Hexadecimal

Kotlin Program to Convert Byte Array to Hexadecimal In this program, you’ll learn different techniques to convert byte array to hexadecimal in Kotlin. Example 1: Convert Byte Array to Hex value fun main(args: Array<String>) { val bytes = byteArrayOf(10, 2, 15, 11) for (b in bytes) { val st = String.format(“%02X”, b) print(st) } } …

Kotlin example for Beginners – Kotlin Program to Convert Array to Set (HashSet) and Vice-Versa

Kotlin Program to Convert Array to Set (HashSet) and Vice-Versa In this program, you’ll learn to convert an array to a set and vice versa in Kotlin. Example 1: Convert Array to Set import java.util.* fun main(args: Array<String>) { val array = arrayOf(“a”, “b”, “c”) val set = HashSet(Arrays.asList(*array)) println(“Set: $set”) } When you run …

Kotlin example for Beginners – Kotlin Program to Convert Map (HashMap) to List

Kotlin Program to Convert Map (HashMap) to List In this program, you’ll learn different techniques to convert a map to a list in Kotlin. Example: Convert Map to List import java.util.ArrayList import java.util.HashMap fun main(args: Array<String>) { val map = HashMap<Int, String>() map.put(1, “a”) map.put(2, “b”) map.put(3, “c”) map.put(4, “d”) map.put(5, “e”) val keyList = …

Kotlin example for Beginners – Kotlin Program to Get Current Working Directory

Kotlin Program to Get Current Working Directory In this program, you’ll learn to get the current working directory in Kotlin. Example 1: Get current working directory fun main(args: Array<String>) { val path = System.getProperty(“user.dir”) println(“Working Directory = $path”) } When you run the program, the output will be: Working Directory = C:UsersAdminDesktopcurrDir In the above …

Kotlin example for Beginners – Kotlin Program to Add Two Integers

Kotlin Program to Convert List (ArrayList) to Array and Vice-Versa In this program, you’ll learn to convert a list to an array using toArray() and array to list using asList() in Kotlin. Example 1: Convert array list to array fun main(args: Array<String>) {   // an arraylist of vowels val vowels_list: List<String> = listOf(“a”, “e”, …