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 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 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 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 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 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 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 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 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”, …
Kotlin Program to Check if An Array Contains a Given Value In this program, you’ll learn to check if an array contains a given value in Kotlin. Example 1: Check if Int Array contains a given value fun main(args: Array<String>) { val num = intArrayOf(1, 2, 3, 4, 5) val toFind = 3 var found …
Kotlin Program to Concatenate Two Arrays In this program, you’ll learn to concatenate two arrays in Kotlin using arraycopy and without it. Example 1: Concatenate Two Arrays using arraycopy import java.util.Arrays fun main(args: Array<String>) { val array1 = intArrayOf(1, 2, 3) val array2 = intArrayOf(4, 5, 6) val aLen = array1.size val bLen = array2.size …
Kotlin Program to Convert String to Date In this program, you’ll learn to convert string to date in Kotlin using formatter. Example 1: Convert String to Date using predefined formatters import java.time.LocalDate import java.time.format.DateTimeFormatter fun main(args: Array<String>) { // Format y-M-d or yyyy-MM-d val string = “2017-07-25” val date = LocalDate.parse(string, DateTimeFormatter.ISO_DATE) println(date) } When …