Kotlin example

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

Hits: 17 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 …

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

Hits: 20 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

Hits: 12 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() …

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

Hits: 3 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 …

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

Hits: 74 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

Hits: 4 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 …

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

Hits: 8 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 …

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

Hits: 23 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 …

Kotlin example for Beginners – Kotlin Program to Concatenate Two Arrays

Hits: 26 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 …

Kotlin example for Beginners – Kotlin Program to Print an Array

Hits: 6 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) …

Kotlin example for Beginners – Kotlin Code To Create Pyramid and Pattern

Hits: 161 Kotlin Code To Create Pyramid and Pattern In this program, you’ll learn to create pyramid, half pyramid, inverted pyramid, Pascal’s triangle and Floyd’s triangle sing control statements in Kotlin.   Programs to print triangles using *, numbers and characters Example 1: Program to print half pyramid using * * * * * * …

Kotlin example for Beginners – Kotlin Program to Add Two Complex Numbers

Hits: 4 Kotlin Program to Add Two Complex Numbers by Passing Class to a Function In this program, you’ll learn to add two complex numbers in Kotlin by creating a class named Complex and passing it into a function add(). Example: Add Two Complex Numbers class Complex(internal var real: Double, internal var imag: Double) fun …